From 68f4178980dcf02ad78df07ae55857094e47a74e Mon Sep 17 00:00:00 2001 From: cozis Date: Sat, 6 Nov 2021 14:00:41 +0100 Subject: [PATCH] bug fix --- samples/loop.noja | 2 ++ src/compiler/compile.c | 30 +++++++++++++----------- src/compiler/parse.c | 50 +++++++++++++++++++++------------------- src/compiler/serialize.c | 32 +++++++++++++------------ src/objects/o_map.c | 4 ---- src/runtime/builtins.c | 2 ++ src/runtime/o_nfunc.c | 3 ++- 7 files changed, 65 insertions(+), 58 deletions(-) diff --git a/samples/loop.noja b/samples/loop.noja index 2d18732..28760f2 100644 --- a/samples/loop.noja +++ b/samples/loop.noja @@ -24,3 +24,5 @@ while i < 3; # ------------------------------------- # # ------------------------------------- # + +print(count()); \ No newline at end of file diff --git a/src/compiler/compile.c b/src/compiler/compile.c index 8c954eb..b28f23d 100644 --- a/src/compiler/compile.c +++ b/src/compiler/compile.c @@ -1,18 +1,20 @@ -/* -- WHAT IS THIS FILE? -- - * - * This file implements the routines that transform the AST - * into a list of bytecodes. The functionalities of this file - * are exposed through the `compile` function, that takes as - * input an `AST` and outputs an `Executable`. - * The function that does the heavy lifting is `emit_instr_for_node` - * which walks the tree and writes instructions to the `ExeBuilder`. - * Some semantic errors are catched at this phase, in which - * case, they are reported by filling out the `error` structure - * and aborting. It's also possible that the compilation fails - * bacause of internal errors (which usually means "out of memory"). - * - */ +/* WHAT IS THIS FILE? +** +** This file implements the routines that transform the AST +** into a list of bytecodes. The functionalities of this file +** are exposed through the `compile` function, that takes as +** input an `AST` and outputs an `Executable`. +** +** The function that does the heavy lifting is `emit_instr_for_node` +** which walks the tree and writes instructions to the `ExeBuilder`. +** +** Some semantic errors are catched at this phase, in which +** case, they are reported by filling out the `error` structure +** and aborting. It's also possible that the compilation fails +** bacause of internal errors (which usually means "out of memory"). +** +*/ #include #include diff --git a/src/compiler/parse.c b/src/compiler/parse.c index 36a846b..a3cb1be 100644 --- a/src/compiler/parse.c +++ b/src/compiler/parse.c @@ -1,28 +1,30 @@ -/* -- WHAT IS THIS FILE? -- - * - * This file implements the parser of the language, that transforms - * `Source` objects into `AST` objects. The functionalities of this - * file are exposed throigh the `parse` function. - * - * It's mainly composed by routines that can each parse specific - * parts of a noja source string. For example, `parse_expression` - * parses expressions and `parse_while_statement` parses while statements. - * These functions call each other recursively to parse the source - * and build the abstract syntax tree (AST) that can be then compiled - * into bytecode. If at any point the parsing fails because of an - * external or internal error, then the error is reported and the parsing - * is aborted. - * Since the nodes of the AST always have the same lifetime (they're - * allocated at the same time and die all together), the allocator - * scheme of choise is a bump-pointer allocator. This way each of the - * parsing routines can allocate memory if it need it but doesn't need - * to free it if an error occurres. - * The parsing routines don't operate directly on the source text, but - * on the tokenized version of it. Before parsing a linked list of - * tokens is produced through the `tokenize` function. - * - */ +/* WHAT IS THIS FILE? +** +** This file implements the parser of the language, that transforms +** `Source` objects into `AST` objects. The functionalities of this +** file are exposed throigh the `parse` function. +** +** It's mainly composed by routines that can each parse specific +** parts of a noja source string. For example, `parse_expression` +** parses expressions and `parse_while_statement` parses while statements. +** These functions call each other recursively to parse the source +** and build the abstract syntax tree (AST) that can be then compiled +** into bytecode. If at any point the parsing fails because of an +** external or internal error, then the error is reported and the parsing +** is aborted. +** +** Since the nodes of the AST always have the same lifetime (they're +** allocated at the same time and die all together), the allocator +** scheme of choise is a bump-pointer allocator. This way each of the +** parsing routines can allocate memory if it need it but doesn't need +** to free it if an error occurres. +** +** The parsing routines don't operate directly on the source text, but +** on the tokenized version of it. Before parsing a linked list of +** tokens is produced through the `tokenize` function. +** +*/ #include #include diff --git a/src/compiler/serialize.c b/src/compiler/serialize.c index 6bb3c2f..8f339c7 100644 --- a/src/compiler/serialize.c +++ b/src/compiler/serialize.c @@ -1,19 +1,21 @@ -/* -- WHAT IS THIS FILE? -- - * - * This file implements the routines that serialize the AST - * into JSON format. The JSON manipulation is handled by the - * third party library xJSON (written by me, still). - * The serialization functionality is exposed through the - * `serialize` function, that takes as an `AST` as argument - * and outputs a string of valid JSON. Therefore the xJSON - * dependency isn't exposed to the caller and can be regarded - * as an implementation detail. - * The way the serialization occurres is by converting the - * AST's representation native to the compiler to one native - * to xJSON, an then calling xj_encode on the converted AST. - * - */ +/* WHAT IS THIS FILE? +** +** This file implements the routines that serialize the AST +** into JSON format. The JSON manipulation is handled by the +** third party library xJSON (written by me, still). +** +** The serialization functionality is exposed through the +** `serialize` function, that takes as an `AST` as argument +** and outputs a string of valid JSON. Therefore the xJSON +** dependency isn't exposed to the caller and can be regarded +** as an implementation detail. +** +** The way the serialization occurres is by converting the +** AST's representation native to the compiler to one native +** to xJSON, an then calling xj_encode on the converted AST. +** +*/ #include #include diff --git a/src/objects/o_map.c b/src/objects/o_map.c index 0377ddb..03a6d9c 100644 --- a/src/objects/o_map.c +++ b/src/objects/o_map.c @@ -116,8 +116,6 @@ static Object *select(Object *self, Object *key, Heap *heap, Error *error) // Not the one we wanted. } - int old_i = i; - pert >>= 5; i = (i * 5 + pert + 1) & mask; } @@ -249,8 +247,6 @@ static _Bool insert(Object *self, Object *key, Object *val, Heap *heap, Error *e // Collision. } - int old_i = i; - pert >>= 5; i = (i * 5 + pert + 1) & mask; } diff --git a/src/runtime/builtins.c b/src/runtime/builtins.c index 30a3fb0..857131d 100644 --- a/src/runtime/builtins.c +++ b/src/runtime/builtins.c @@ -11,6 +11,8 @@ static Object *bin_print(Runtime *runtime, Object **argv, unsigned int argc, Err static Object *bin_count(Runtime *runtime, Object **argv, unsigned int argc, Error *error) { + assert(argc == 1); + int n = Object_Count(argv[0], error); if(error->occurred) diff --git a/src/runtime/o_nfunc.c b/src/runtime/o_nfunc.c index 18f591a..cfad689 100644 --- a/src/runtime/o_nfunc.c +++ b/src/runtime/o_nfunc.c @@ -55,7 +55,8 @@ static Object *call(Object *self, Object **argv, unsigned int argc, Heap *heap, { // Some arguments are missing. argv2 = malloc(sizeof(Object*) * expected_argc); - + argc2 = expected_argc; + if(argv2 == NULL) { Error_Report(error, 1, "No memory");