refactor and add comment support
This commit is contained in:
+5
-2
@@ -37,11 +37,14 @@ func_arithmetic(lisp_function_gt_eq, >=);
|
|||||||
|
|
||||||
int lisp_function_println(Nodes args, Scopes *scopes, Node *result) {
|
int lisp_function_println(Nodes args, Scopes *scopes, Node *result) {
|
||||||
if (args.count != 1) {
|
if (args.count != 1) {
|
||||||
printf("'+' expects one argument!\n");
|
printf("'println' expects one argument!\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
eval_one(args.items[0], scopes, result);
|
int ret = eval_one(args.items[0], scopes, result);
|
||||||
|
if (ret != 0)
|
||||||
|
return ret;
|
||||||
|
|
||||||
node_print(*result);
|
node_print(*result);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
#include "lisp.h"
|
#include "lisp.h"
|
||||||
|
|
||||||
static Scope globalScope = {0};
|
static Scope global_scope = {0};
|
||||||
|
|
||||||
static int scope_lookup(Scopes *scopes, Nob_String_View name, Node *result) {
|
static int scope_lookup(Scopes *scopes, Nob_String_View name, Node *result) {
|
||||||
nob_da_foreach(Scope *, scope, scopes) {
|
nob_da_foreach(Scope *, scope, scopes) {
|
||||||
nob_da_foreach(ScopeObject, scopeObject, *scope) {
|
nob_da_foreach(ScopeObject, scope_object, *scope) {
|
||||||
if (nob_sv_eq(name, scopeObject->name)) {
|
if (nob_sv_eq(name, scope_object->name)) {
|
||||||
*result = scopeObject->node;
|
*result = scope_object->node;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,26 +39,26 @@ static bool node_is_true(Node node) {
|
|||||||
|
|
||||||
void build_scopes(Scopes *scopes) {
|
void build_scopes(Scopes *scopes) {
|
||||||
// clang-format off
|
// clang-format off
|
||||||
scope_add_function(&globalScope, "+", lisp_function_add);
|
scope_add_function(&global_scope, "+", lisp_function_add);
|
||||||
scope_add_function(&globalScope, "-", lisp_function_sub);
|
scope_add_function(&global_scope, "-", lisp_function_sub);
|
||||||
scope_add_function(&globalScope, "/", lisp_function_div);
|
scope_add_function(&global_scope, "/", lisp_function_div);
|
||||||
scope_add_function(&globalScope, "*", lisp_function_mul);
|
scope_add_function(&global_scope, "*", lisp_function_mul);
|
||||||
scope_add_function(&globalScope, "%", lisp_function_mod);
|
scope_add_function(&global_scope, "%", lisp_function_mod);
|
||||||
scope_add_function(&globalScope, "=", lisp_function_eq);
|
scope_add_function(&global_scope, "=", lisp_function_eq);
|
||||||
scope_add_function(&globalScope, "<", lisp_function_lt);
|
scope_add_function(&global_scope, "<", lisp_function_lt);
|
||||||
scope_add_function(&globalScope, ">", lisp_function_gt);
|
scope_add_function(&global_scope, ">", lisp_function_gt);
|
||||||
scope_add_function(&globalScope, "<=", lisp_function_lt_eq);
|
scope_add_function(&global_scope, "<=", lisp_function_lt_eq);
|
||||||
scope_add_function(&globalScope, ">=", lisp_function_gt_eq);
|
scope_add_function(&global_scope, ">=", lisp_function_gt_eq);
|
||||||
|
|
||||||
scope_add_function(&globalScope, "println", lisp_function_println);
|
scope_add_function(&global_scope, "println", lisp_function_println);
|
||||||
scope_add_function(&globalScope, "list", lisp_function_list);
|
scope_add_function(&global_scope, "list", lisp_function_list);
|
||||||
scope_add_function(&globalScope, "append", lisp_function_append);
|
scope_add_function(&global_scope, "append", lisp_function_append);
|
||||||
scope_add_function(&globalScope, "length", lisp_function_length);
|
scope_add_function(&global_scope, "length", lisp_function_length);
|
||||||
scope_add_function(&globalScope, "null?", lisp_function_isnull);
|
scope_add_function(&global_scope, "null?", lisp_function_isnull);
|
||||||
scope_add_function(&globalScope, "builtins", lisp_function_builtins);
|
scope_add_function(&global_scope, "builtins", lisp_function_builtins);
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
nob_da_append(scopes, &globalScope);
|
nob_da_append(scopes, &global_scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *token_kind_name(TokenKind kind) {
|
const char *token_kind_name(TokenKind kind) {
|
||||||
@@ -103,12 +103,26 @@ void node_print(Node node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void tokenize(Nob_String_View content, Tokens *tokens) {
|
int tokenize(Nob_String_View content, Tokens *tokens) {
|
||||||
|
bool ignored = false;
|
||||||
for (size_t i = 0; i < content.count; i++) {
|
for (size_t i = 0; i < content.count; i++) {
|
||||||
char c = content.data[i];
|
char c = content.data[i];
|
||||||
|
|
||||||
|
if (ignored) {
|
||||||
|
if (c == '\n')
|
||||||
|
ignored = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
int kind = -1;
|
int kind = -1;
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
case '/': {
|
||||||
|
if (i + 1 < content.count && content.items[i + 1] == '/') {
|
||||||
|
i++;
|
||||||
|
ignored = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
case '(': {
|
case '(': {
|
||||||
kind = TOKEN_KIND_PAR_LEFT;
|
kind = TOKEN_KIND_PAR_LEFT;
|
||||||
} break;
|
} break;
|
||||||
@@ -156,6 +170,7 @@ void tokenize(Nob_String_View content, Tokens *tokens) {
|
|||||||
nob_da_append(tokens, tok);
|
nob_da_append(tokens, tok);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_one(Tokens *tokens, Node *node) {
|
int parse_one(Tokens *tokens, Node *node) {
|
||||||
@@ -325,7 +340,7 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
nob_da_foreach(ScopeObject, scope_object, &globalScope) {
|
nob_da_foreach(ScopeObject, scope_object, &global_scope) {
|
||||||
if (nob_sv_eq(scope_object->name, name_node.as_symbol)) {
|
if (nob_sv_eq(scope_object->name, name_node.as_symbol)) {
|
||||||
scope_object->node = value;
|
scope_object->node = value;
|
||||||
*result = children->items[0 + 1];
|
*result = children->items[0 + 1];
|
||||||
@@ -335,7 +350,7 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
|
|||||||
|
|
||||||
ScopeObject obj =
|
ScopeObject obj =
|
||||||
(ScopeObject){.name = name_node.as_symbol, .node = value};
|
(ScopeObject){.name = name_node.as_symbol, .node = value};
|
||||||
nob_da_append(&globalScope, obj);
|
nob_da_append(&global_scope, obj);
|
||||||
*result = children->items[0 + 1];
|
*result = children->items[0 + 1];
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ const char *token_kind_name(TokenKind kind);
|
|||||||
void node_print(Node node);
|
void node_print(Node node);
|
||||||
|
|
||||||
void build_scopes(Scopes *scopes);
|
void build_scopes(Scopes *scopes);
|
||||||
void tokenize(Nob_String_View content, Tokens *tokens);
|
int tokenize(Nob_String_View content, Tokens *tokens);
|
||||||
|
|
||||||
int parse_one(Tokens *tokens, Node *node);
|
int parse_one(Tokens *tokens, Node *node);
|
||||||
int parse(Tokens *tokens, Nodes *nodes);
|
int parse(Tokens *tokens, Nodes *nodes);
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Tokens tokens = {0};
|
Tokens tokens = {0};
|
||||||
tokenize(view, &tokens);
|
if (tokenize(view, &tokens) != 0)
|
||||||
|
return 1;
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
for (size_t i = 0; i < tokens.count; i++) {
|
for (size_t i = 0; i < tokens.count; i++) {
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
// wadadada
|
||||||
|
|
||||||
|
(list 1 2 3)
|
||||||
Reference in New Issue
Block a user