From: Mike Lowis Date: Tue, 30 Aug 2016 19:48:17 +0000 (-0400) Subject: Added token type for ':' in preparation for type annotations X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=be01f65d785751f7e14a99ccc212f105db3587ac;p=proto%2Fsclpl.git Added token type for ':' in preparation for type annotations --- diff --git a/source/ast.c b/source/ast.c index 561bef3..7f2100f 100644 --- a/source/ast.c +++ b/source/ast.c @@ -2,8 +2,6 @@ static void ast_free(void* ptr) { -// AST_LET AST_TEMP - AST* ast = (AST*)ptr; switch(ast->type) { case AST_REQ: diff --git a/source/lexer.l b/source/lexer.l index 13cc635..9f949a2 100644 --- a/source/lexer.l +++ b/source/lexer.l @@ -42,6 +42,7 @@ NOSPACE [^ \t\r\n] ";" { return T_END; } "," { return T_COMMA; } "'" { return T_SQUOTE; } +":" { return T_COLON; } \\. { Value.character = yytext[1]; return T_CHAR; } \\space { Value.character = ' '; return T_CHAR; } @@ -115,12 +116,11 @@ false { return T_ID; } -[^ \r\t\n\[\]\{\}\(\)'\",;]+ { +[^ \r\t\n\[\]\{\}\(\)'\",;:]+ { Value.text = dupstring(yytext); return T_ID; } - %% static void token_free(void* obj) diff --git a/source/pprint.c b/source/pprint.c index 41bf2f3..048e7df 100644 --- a/source/pprint.c +++ b/source/pprint.c @@ -27,6 +27,7 @@ static const char* token_type_to_string(TokType type) { case T_COMMA: return "T_COMMA"; case T_ID: return "T_ID"; case T_END: return "T_END"; + case T_COLON: return "T_COLON"; case T_SQUOTE: return "T_SQUOTE"; case T_DQUOTE: return "T_DQUOTE"; case T_END_FILE: return "T_END_FILE"; diff --git a/source/sclpl.h b/source/sclpl.h index cd6a437..64fff92 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -51,8 +51,8 @@ void vec_set(vec_t* vec, size_t index, void* data); *****************************************************************************/ typedef enum { 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_REQUIRE, - T_DEF, T_IF, T_FN, T_THEN, T_ELSE, T_END_FILE + T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE, T_DQUOTE, T_END, T_COLON, + T_REQUIRE, T_DEF, T_IF, T_FN, T_THEN, T_ELSE, T_END_FILE } TokType; typedef struct { @@ -190,6 +190,68 @@ AST* let_val(AST* let); AST* let_body(AST* let); void let_set_body(AST* let, AST* body); +/* Symbol Table + *****************************************************************************/ +typedef struct SymTable { + struct SymTable* next; + char* name; +} SymTable; + +SymTable* symbol_new(void); +//void symbol_free(SymTable* sym); +SymTable* symbol_push(SymTable* top, SymTable* newtop); +SymTable* symbol_pop(SymTable* top); +SymTable* symbol_get(const char* name); +SymTable* symbol_map(SymTable* top, void (*apply)(SymTable*, void*), void* arg); + +/* +Base Types: + bool + int + uint + u8 u16 u32 u64 + i8 i16 i32 i64 + uintptr intptr + rune + byte + string + +Reference: + u8& + +Array: + u8[] u8[10] u8[10][10] + +Function: + int (string[]) + +Struct: + +Union: + + +Examples: +def main(args) + return 0; +end + +def main(args : string[]) : int + return 0; +end + +def main : int(string[]) + fn(args) + + end +end + +def foo (123 : int); +def foo : int 123; +def foo 123; + +*/ + + /* Pretty Printing *****************************************************************************/ void pprint_token_type(FILE* file, Tok* token); diff --git a/spec/lexer_spec.rb b/spec/lexer_spec.rb index 65d9711..030bb2e 100644 --- a/spec/lexer_spec.rb +++ b/spec/lexer_spec.rb @@ -38,6 +38,10 @@ describe "lexer" do expect(lexer(';')).to eq ['T_END'] end + it "should recognize :" do + expect(lexer(':')).to eq ['T_COLON'] + end + it "should recognize all punctuation" do expect(lexer('[](){}\',;')).to eq( ["T_LBRACK", "T_RBRACK", "T_LPAR", "T_RPAR", "T_LBRACE", "T_RBRACE",