Skip to content

Commit

Permalink
Add class declarations, new expressions, and string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk committed Sep 14, 2015
1 parent 90cc5b3 commit 9be8980
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/grammar.jison
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* Lexer ------------------------------------------------------------------ */

%lex

StringEscapeSequence \\[\'\"\\bfnrtv]
StringCharacter ([^\"\\\n\r]+)|({StringEscapeSequence})
StringLiteral \"{StringCharacter}\"

%%

[ \t]+ /* Skip whitespace */
"#".*($|\r\n|\r|\n) /* Skip commments */
"func" return 'FUNC';
"new" return 'NEW';
"class" return 'CLASS';
"init" return 'INIT';
"return" return 'RETURN';
"let" return 'LET';
"var" return 'VAR';
Expand All @@ -17,6 +24,7 @@
"null" return 'NULL';
[A-Za-z][A-Za-z0-9_]* return 'WORD';
0|([1-9][0-9]*) return 'NUMBER';
{StringLiteral} return 'STRING';
\n return 'NEWLINE';
"->" return '->';
"==" return '==';
Expand Down Expand Up @@ -85,7 +93,9 @@ block_statements

statement
: declaration_statement
| class_statement
| function_statement
| init_statement
| return_statement
| condition_statement
| assignment_statement
Expand All @@ -99,12 +109,18 @@ return_statement

function_statement
: FUNC identifier function_declaration
{ var f = $3;
f.name = $2;
$$ = f;
{ var f = $3;
f.name = $2;
$$ = f;
}
;

init_statement
: INIT function_parameters block
{ $$ = new yy.AST.Init($2, $3);
}
;

function_declaration
: function_parameters function_return block
{ var f = new yy.AST.Function($1, $2, $3);
Expand Down Expand Up @@ -199,6 +215,10 @@ declaration_type
| VAR { $$ = yy.AST.Var; }
;

class_statement
: CLASS WORD block { $$ = new yy.AST.Class($2, $3); }
;

assignment_statement
: expression '=' expression
{ $$ = new yy.AST.Assignment('path', $1, '=', $3); }
Expand Down Expand Up @@ -265,9 +285,15 @@ postfix_expression_list
primary_expression
: '(' expression ')' { $$ = $2; }
| FUNC function_declaration { $$ = $2; }
| new_expression { $$ = $1; }
| atom { $$ = $1; }
;

new_expression
: NEW WORD '(' ')' { $$ = new yy.AST.New($2, []); }
| NEW WORD '(' call_arguments ')' { $$ = new yy.AST.New($2, $4); }
;

call
: '(' ')' { $$ = new yy.AST.Call([]); }
| '(' call_arguments ')' { $$ = new yy.AST.Call($2); }
Expand Down Expand Up @@ -311,6 +337,7 @@ name_type
atom
: identifier
| number
| string
;

identifier
Expand All @@ -321,6 +348,14 @@ number
: NUMBER { $$ = new yy.AST.Literal(parseInt($1, 10), 'Integer'); }
;

string
: STRING
{ var s = $1;
s = s.slice(1, s.length - 1); // Remove the surrounding quotes
$$ = new yy.AST.Literal(s, 'String');
}
;

terminal
: terminal terminal_token
| terminal_token
Expand Down

0 comments on commit 9be8980

Please sign in to comment.