Skip to content

Commit

Permalink
Merge pull request #416 from dictu-lang/develop
Browse files Browse the repository at this point in the history
Release 0.20.0
  • Loading branch information
Jason2605 authored Aug 8, 2021
2 parents a36dbea + e42318c commit e07b84d
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 14 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,10 @@ in the [DictuVSC repo](https://github.com/dictu-lang/DictuVSC).
## Credits

This language was initially based on the very good [craftinginterpreters book](http://www.craftinginterpreters.com/contents.html), along with inspiration from [Wren](https://github.com/wren-lang/wren).

<p>This project is supported by:</p>
<p>
<a href="https://m.do.co/c/02bd923f5cda">
<img src="https://opensource.nyc3.cdn.digitaloceanspaces.com/attribution/assets/SVG/DO_Logo_horizontal_blue.svg" width="201px">
</a>
</p>
2 changes: 1 addition & 1 deletion docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: >-
color_scheme: "dictu" # Custom theme
logo: "/assets/images/dictu-logo/dictu-wordmark.svg"

version: "0.19.0"
version: "0.20.0"
github_username: dictu-lang
search_enabled: true

Expand Down
13 changes: 13 additions & 0 deletions docs/docs/collections/dictionaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,17 @@ This is the exact same scenario as lists, so refer to [copying Lists](#copying-l
var myDict = {"key": {"test": 10}};
var myDict1 = myDict.copy(); // Shallow copy
var myDict2 = myDict.deepCopy(); // Deep copy
```

### dict.forEach(func)

To run a function on every element in a dictionary we use `.forEach`. The callback function
passed to `.forEach` expects two parameters which will be the key-value pair of the dictionary.

```cs
const myDict = {"key": 1, "key1": 2};

myDict.forEach(def (key, value) => {
print("Key: {} Value: {}".format(key, value));
});
```
17 changes: 17 additions & 0 deletions docs/docs/thank-you.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
layout: default
title: Thank you!
nav_order: 16
---

# Thank You!
{: .no_toc }

## DigitalOcean

Dictu has been proudly sponsored by [DigitalOcean](https://m.do.co/c/02bd923f5cda). They are providing the project with a generous amount
of credits so that testing of the Dictu VM can be carried out across a number of different platform versions along with providing the use of
tooling that comes with these specific platforms.

I've personally been using their services for many years prior to this sponsorship so can attest to their excellent service, so if you too wish
to sign up, you can do so via [this](https://m.do.co/c/02bd923f5cda) referral link!
2 changes: 1 addition & 1 deletion src/include/dictu_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdbool.h>

#define DICTU_MAJOR_VERSION "0"
#define DICTU_MINOR_VERSION "19"
#define DICTU_MINOR_VERSION "20"
#define DICTU_PATCH_VERSION "0"

#define DICTU_STRING_VERSION "Dictu Version: " DICTU_MAJOR_VERSION "." DICTU_MINOR_VERSION "." DICTU_PATCH_VERSION "\n"
Expand Down
14 changes: 14 additions & 0 deletions src/vm/datatypes/dicts/dict-source.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#define DICTU_DICT_SOURCE "/**\n" \
" * This file contains all the methods for Dicts written in Dictu that\n" \
" * are unable to be written in C due to re-enterability issues.\n" \
" *\n" \
" * We should always strive to write methods in C where possible.\n" \
" */\n" \
"def forEach(dict, func) {\n" \
" const dictKeys = dict.keys();\n" \
"\n" \
" for (var i = 0; i < dictKeys.len(); i += 1) {\n" \
" func(dictKeys[i], dict[dictKeys[i]]);\n" \
" }\n" \
"}\n" \

13 changes: 13 additions & 0 deletions src/vm/datatypes/dicts/dict.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* This file contains all the methods for Dicts written in Dictu that
* are unable to be written in C due to re-enterability issues.
*
* We should always strive to write methods in C where possible.
*/
def forEach(dict, func) {
const dictKeys = dict.keys();

for (var i = 0; i < dictKeys.len(); i += 1) {
func(dictKeys[i], dict[dictKeys[i]]);
}
}
20 changes: 20 additions & 0 deletions src/vm/datatypes/dicts.c → src/vm/datatypes/dicts/dicts.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#include "dicts.h"

/*
* Note: We should try to implement everything we can in C
* rather than in the host language as C will always
* be faster than Dictu, and there will be extra work
* at startup running the Dictu code, however compromises
* must be made due to the fact the VM is not re-enterable
*/

#include "dict-source.h"

static Value toStringDict(DictuVM *vm, int argCount, Value *args) {
if (argCount != 0) {
runtimeError(vm, "toString() takes no arguments (%d given)", argCount);
Expand Down Expand Up @@ -156,4 +166,14 @@ void declareDictMethods(DictuVM *vm) {
defineNative(vm, &vm->dictMethods, "copy", copyDictShallow);
defineNative(vm, &vm->dictMethods, "deepCopy", copyDictDeep);
defineNative(vm, &vm->dictMethods, "toBool", boolNative); // Defined in util

dictuInterpret(vm, "Dict", DICTU_DICT_SOURCE);

Value Dict;
tableGet(&vm->modules, copyString(vm, "Dict", 4), &Dict);

ObjModule *DictModule = AS_MODULE(Dict);
push(vm, Dict);
tableAddAll(vm, &DictModule->values, &vm->dictMethods);
pop(vm);
}
4 changes: 2 additions & 2 deletions src/vm/datatypes/dicts.h → src/vm/datatypes/dicts/dicts.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <string.h>
#include <stdlib.h>

#include "copy.h"
#include "../util.h"
#include "../copy.h"
#include "../../util.h"

void declareDictMethods(DictuVM *vm);

Expand Down
1 change: 1 addition & 0 deletions src/vm/datatypes/generate.du
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def generate(filename) {

var files = [
'lists/list',
'dicts/dict',
'result/result'
];

Expand Down
16 changes: 8 additions & 8 deletions src/vm/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,32 +537,32 @@ char *objectToString(Value value) {
char *methodString;

if (method->method->function->name != NULL) {
methodString = malloc(sizeof(char) * (method->method->function->name->length + 17));

switch (method->method->function->type) {
case TYPE_STATIC: {
snprintf(methodString, method->method->function->name->length + 17, "<Bound Method %s>", method->method->function->name->chars);
methodString = malloc(sizeof(char) * (method->method->function->name->length + 17));
snprintf(methodString, method->method->function->name->length + 17, "<Static Method %s>", method->method->function->name->chars);
break;
}

default: {
snprintf(methodString, method->method->function->name->length + 17, "<Static Method %s>", method->method->function->name->chars);
methodString = malloc(sizeof(char) * (method->method->function->name->length + 16));
snprintf(methodString, method->method->function->name->length + 16, "<Bound Method %s>", method->method->function->name->chars);
break;
}
}
} else {
methodString = malloc(sizeof(char) * 16);

switch (method->method->function->type) {
case TYPE_STATIC: {
methodString = malloc(sizeof(char) * 16);
memcpy(methodString, "<Static Method>", 15);
methodString[15] = '\0';
break;
}

default: {
memcpy(methodString, "<Bound Method>", 15);
methodString[15] = '\0';
methodString = malloc(sizeof(char) * 15);
memcpy(methodString, "<Bound Method>", 14);
methodString[14] = '\0';
break;
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "datatypes/nil.h"
#include "datatypes/strings.h"
#include "datatypes/lists/lists.h"
#include "datatypes/dicts.h"
#include "datatypes/dicts/dicts.h"
#include "datatypes/sets.h"
#include "datatypes/files.h"
#include "datatypes/class.h"
Expand Down Expand Up @@ -501,7 +501,17 @@ static bool invoke(DictuVM *vm, ObjString *name, int argCount) {
case OBJ_DICT: {
Value value;
if (tableGet(&vm->dictMethods, name, &value)) {
return callNativeMethod(vm, value, argCount);
if (IS_NATIVE(value)) {
return callNativeMethod(vm, value, argCount);
}

push(vm, peek(vm, 0));

for (int i = 2; i <= argCount + 1; i++) {
vm->stackTop[-i] = peek(vm, i);
}

return call(vm, AS_CLOSURE(value), argCount + 1);
}

runtimeError(vm, "Dict has no method %s().", name->chars);
Expand Down
27 changes: 27 additions & 0 deletions tests/dicts/forEach.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* forEach.du
*
* Testing the dict.forEach() method
*
* .forEach() loops over a dictionary
*/

const myDict = {"key": 1, "key1": 2, "key2": 3};
var count = 0;

myDict.forEach(def (key, value) => {
count += 1;
assert(type(key) == "string");
assert(type(value) == "number");
});

assert(count == 3);

const myNewDict = {1: 1, 2: 2, 3: 3};
var sum = 0;

myNewDict.forEach(def (key, value) => {
sum += key + value;
});

assert(sum == 12);
1 change: 1 addition & 0 deletions tests/dicts/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ import "copy.du";
import "len.du";
import "toString.du";
import "toBool.du";
import "forEach.du";

0 comments on commit e07b84d

Please sign in to comment.