]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
minor refactoring and added = token
authorMichael D. Lowis <mike@mdlowis.com>
Thu, 24 May 2018 02:18:37 +0000 (22:18 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Thu, 24 May 2018 02:18:37 +0000 (22:18 -0400)
source/ast.c
source/lexer.l
source/parser.c
source/sclpl.h

index f105c50cff991bc6836bdc068fb011469d74040c..7891139b75dd00450842e9db16caf1a4276ffbcb 100644 (file)
 #include <sclpl.h>
 
-static AST* ast(ASTType type)
-{
+static AST* ast(ASTType type) {
     AST* tree = emalloc(sizeof(AST));
     memset(tree, 0, sizeof(AST));
     tree->type = type;
     return tree;
 }
 
-AST* String(Tok* val)
-{
+AST* String(Tok* val) {
     AST* node = ast(AST_STRING);
     node->value.text = val->value.text;
     return node;
 }
 
-char* string_value(AST* val)
-{
-    assert(val != NULL);
+char* string_value(AST* val) {
     assert(val->type == AST_STRING);
     return val->value.text;
 }
 
-AST* Symbol(Tok* val)
-{
+AST* Symbol(Tok* val) {
     AST* node = ast(AST_SYMBOL);
     node->value.text = val->value.text;
     return node;
 }
 
-char* symbol_value(AST* val)
-{
-    assert(val != NULL);
+char* symbol_value(AST* val) {
     assert(val->type == AST_SYMBOL);
     return val->value.text;
 }
 
-AST* Char(Tok* val)
-{
+AST* Char(Tok* val) {
     AST* node = ast(AST_CHAR);
     node->value.character = val->value.character;
     return node;
 }
 
-uint32_t char_value(AST* val)
-{
-    assert(val != NULL);
+uint32_t char_value(AST* val) {
     assert(val->type == AST_CHAR);
     return val->value.character;
 }
 
-AST* Integer(Tok* val)
-{
+AST* Integer(Tok* val) {
     AST* node = ast(AST_INT);
     node->value.integer = val->value.integer;
     return node;
 }
 
-intptr_t integer_value(AST* val)
-{
-    assert(val != NULL);
+intptr_t integer_value(AST* val) {
     assert(val->type == AST_INT);
     return val->value.integer;
 }
 
-intptr_t temp_value(AST* val)
-{
-    assert(val != NULL);
-    return val->value.integer;
-}
-
-AST* Float(Tok* val)
-{
+AST* Float(Tok* val) {
     AST* node = ast(AST_FLOAT);
     node->value.floating = val->value.floating;
     return node;
 }
 
-double float_value(AST* val)
-{
-    assert(val != NULL);
+double float_value(AST* val) {
     assert(val->type == AST_FLOAT);
     return val->value.floating;
 }
 
-AST* Bool(Tok* val)
-{
+AST* Bool(Tok* val) {
     AST* node = ast(AST_BOOL);
     node->value.boolean = val->value.boolean;
     return node;
 }
 
-bool bool_value(AST* val)
-{
-    assert(val != NULL);
+bool bool_value(AST* val) {
     assert(val->type == AST_BOOL);
     return val->value.boolean;
 }
 
-AST* Ident(Tok* val)
-{
+AST* Ident(Tok* val) {
     AST* node = ast(AST_IDENT);
     node->value.text = val->value.text;
     return node;
 }
 
-char* ident_value(AST* val)
-{
-    assert(val != NULL);
+char* ident_value(AST* val) {
     assert(val->type == AST_IDENT);
     return val->value.text;
 }
 
-AST* Let(Tok* name, AST* value)
-{
+AST* Let(Tok* name, AST* value) {
     AST* node = ast(AST_LET);
     node->value.let.name = name->value.text;
     node->value.let.value = value;
     return node;
 }
 
-char* let_name(AST* let)
-{
-    assert(let != NULL);
+char* let_name(AST* let) {
     assert(let->type == AST_LET);
     return let->value.let.name;
 }
 
-AST* let_value(AST* let)
-{
-    assert(let != NULL);
+AST* let_value(AST* let) {
     assert(let->type == AST_LET);
     return let->value.let.value;
 }
-
index 89931cb5dde852bfd641adb36aef7a830481479b..9105367226e408b367b4c34708f78f01b5cc13db 100644 (file)
@@ -48,6 +48,7 @@ NOSPACE [^ \t\r\n]
 "'"    { return T_SQUOTE; }
 ":"    { return T_COLON;  }
 "&"    { return T_AMP;    }
+"="    { return T_ASSIGN; }
 
 \\.       { Value.character = yytext[1];    return T_CHAR; }
 \\space   { Value.character = ' ';          return T_CHAR; }
index 37114109b7bd260086775c8e1ab1f268bca2618c..8e47f1c1062596472b697b633b1bcd12f9543993 100644 (file)
@@ -1,14 +1,10 @@
 #include <sclpl.h>
 
-/* Private Declarations
- *****************************************************************************/
-// Grammar Routines
 static AST* const_definition(Parser* p);
 static AST* const_expression(Parser* p);
 static AST* definition(Parser* p);
 static AST* expression(Parser* p);
 static AST* identifier(Parser* p);
-
 static AST* function(Parser* p);
 static void type_annotation(Parser* p);
 static AST* literal(Parser* p);
@@ -81,6 +77,7 @@ static AST* const_definition(Parser* p) {
 //        expr = function(p);
 //    } else {
         type_annotation(p);
+        expect(p, T_ASSIGN);
         expr = const_expression(p);
         expect(p, T_END);
 //    }
@@ -153,122 +150,3 @@ static AST* identifier(Parser* p) {
         return NULL;
     }
 }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-#if 0
-static AST* definition(Parser* p) {
-    AST* expr;
-    Tok* id = expect_val(p, T_ID);
-//    if (peek(p)->type == T_LPAR) {
-//        expr = function(p);
-//    } else {
-        type_annotation(p);
-        expr = expression(p);
-        expect(p, T_END);
-//    }
-    return Def(id, expr);
-}
-
-static AST* expression(Parser* p) {
-    AST* expr = NULL;
-    if (accept(p, T_LPAR)) {
-        expr = expression(p);
-        expect(p, T_RPAR);
-    } else if (accept(p, T_IF)) {
-        expr = if_stmnt(p);
-    } else if (match(p, T_ID)) {
-        expr = Ident(expect_val(p,T_ID));
-    } else {
-        expr = literal(p);
-    }
-    /* Check if this is a function application */
-    if (peek(p)->type == T_LPAR)
-        expr = func_app(p, expr);
-    return expr;
-}
-
-/*
-static AST* function(Parser* p) {
-    AST* func = Func();
-    expect(p, T_LPAR);
-    while(peek(p)->type != T_RPAR) {
-        func_add_arg(func, Ident(expect_val(p,T_ID)));
-        type_annotation(p);
-        if(peek(p)->type != T_RPAR)
-            expect(p, T_COMMA);
-    }
-    expect(p, T_RPAR);
-    type_annotation(p);
-    func_set_body(func, expr_block(p));
-    expect(p, T_END);
-    return func;
-}
-*/
-
-static AST* expr_block(Parser* p) {
-    AST* block = NULL;
-    vec_t exprs;
-    vec_init(&exprs);
-    /* Build all expressions into let forms with no bodies */
-    do {
-        if (accept(p, T_LET)) {
-            AST* def = definition(p);
-            Tok name = { .value.text = def_name(def) };
-            vec_push_back(&exprs, Let(Ident(&name), def_value(def), NULL));
-        } else {
-            vec_push_back(&exprs, Let(TempVar(), expression(p), NULL));
-        }
-    } while(!match(p, T_END) && !match(p, T_ELSE));
-    /* Now nest all of the let forms making sure that the last one returns
-     * it's definition as its body */
-    for (int i = vec_size(&exprs); i > 0; i--) {
-        AST* let = (AST*)vec_at(&exprs,i-1);
-        let_set_body(let, (block == NULL) ? let_var(let) : block);
-        block = let;
-    }
-    vec_deinit(&exprs);
-    return block;
-}
-
-static AST* if_stmnt(Parser* p) {
-    AST* ifexpr = IfExpr();
-    ifexpr_set_cond( ifexpr, expression(p) );
-    accept(p, T_THEN);
-    ifexpr_set_then( ifexpr, expr_block(p) );
-    if (accept(p, T_ELSE))
-        ifexpr_set_else( ifexpr, expr_block(p) );
-    expect(p,T_END);
-    return ifexpr;
-}
-
-static AST* func_app(Parser* p, AST* fn) {
-    AST* app = FnApp(fn);
-    expect(p,T_LPAR);
-    while (peek(p)->type != T_RPAR) {
-        fnapp_add_arg(app, expression(p));
-        if (peek(p)->type != T_RPAR)
-            expect(p, T_COMMA);
-    }
-    expect(p,T_RPAR);
-    return app;
-}
-#endif
index c72b9fb539b9238d2c06117936a2337483d2ad58..9a84205a05b381c3a3aaed6671367da693b6f98e 100644 (file)
@@ -40,9 +40,9 @@ void vec_set(vec_t* vec, size_t index, void* data);
 /* Token Types
  *****************************************************************************/
 typedef enum {
-    T_NONE, T_ERROR, T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING, T_LBRACE,
-    T_RBRACE, T_LBRACK, T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE, T_DQUOTE,
-    T_END, T_COLON, T_AMP, T_LET, T_IF, T_THEN, T_ELSE, T_END_FILE
+    T_NONE, T_ERROR, T_END_FILE, T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING,
+    T_LBRACE, T_RBRACE, T_LBRACK, T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE,
+    T_DQUOTE, T_END, T_COLON, T_AMP, T_LET, T_IF, T_THEN, T_ELSE, T_ASSIGN
 } TokType;
 
 typedef struct {