X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/45409c781a9e35df68c43b1e2f028d30bf90c0a0..8aeae09e74b46ca52866f22b48f55fecdf27b849:/Toc.g4 diff --git a/Toc.g4 b/Toc.g4 index 95c9a0d..afe4209 100644 --- a/Toc.g4 +++ b/Toc.g4 @@ -2,81 +2,92 @@ grammar Toc; prog: (decl)+ EOF; -decl: varDecl +decl: varDecl ';' | funcDecl | structDecl ; varDecl: 'var' var; var: varName (':' type) ('=' expr)?; +varInit: varName (':' type) ('=' expr); -type: typeName; +type: typeName (typeModifier)*; +typeModifier: '*' | ('[' (INT_LIT)? ']'); funcDecl: 'func' func; func: funcName '(' parameter ')' (':' type) body; -parameter: (firstParameter (additionalParameter)*)?; -firstParameter: var; -additionalParameter: ',' var; +parameter: (var (',' var)*)?; body: '{' stmt* '}'; structDecl: 'struct' structName '{' structMember* '}'; structMember: structVar | structMethod; -structVar: var; +structVar: var ';'; structMethod: func; -stmt: (varDecl - | conditional - | loop - | assignment - | returnStmt - | expr) ; +stmt: varDecl ';' + | ifStmt + | switchStmt + | forStmt + | whileStmt + | assignStmt ';' + | returnStmt ';' + | expr ';'; -conditional: ifCond; -ifCond: 'if' expr body; +ifStmt: 'if' expr body elseIfStmt* elseStmt?; +elseIfStmt: 'else' 'if' expr body; +elseStmt: 'else' body; -loop: whileLoop; -whileLoop: 'while' expr body; +switchStmt: 'switch' identifierExpr switchBody; +switchBody: '{' switchCase* '}'; +switchCase: 'case' expr body; -assignment: identifier '=' expr; +forStmt: 'for' (varInit | assignStmt) ',' expr ',' expr body; + +whileStmt: 'while' expr body; + +assignStmt: identifierExpr '=' expr; returnStmt: 'return' expr; -expr: funcCall - | literal - | identifier - | subscript - | memberAccess +expr: funcExpr + | litExpr + | identifierExpr | parenExpr - | operatorExpr; - -nonOpExpr: funcCall - | literal - | identifier - | subscript - | memberAccess - | parenExpr; + | accessExpr + | opExpr; -nonSubscriptExpr: funcCall - | identifier - | memberAccess - | parenExpr; +/* op */ +nonOpExpr: funcExpr + | litExpr + | identifierExpr + | parenExpr + | accessExpr; -funcCall: funcName '(' (expr (',' expr)*)? ')'; +/* lit access op */ +nonAccessExpr: funcExpr + | identifierExpr + | parenExpr; -operatorExpr: binaryOperator; -binaryOperator: nonOpExpr BINARY_OPERATOR nonOpExpr (BINARY_OPERATOR nonOpExpr)*; +funcExpr: funcName '(' (expr (',' expr)*)? ')'; -identifier: varName; +opExpr: binaryOp | prefixOp | postfixOp | ternaryOp; +binaryOp: nonOpExpr binary_op nonOpExpr (binary_op nonOpExpr)*; +prefixOp: prefix_op nonOpExpr; +postfixOp: nonOpExpr postfix_op; +ternaryOp: nonOpExpr '?' expr ':' expr; -literal: INTLIT; +identifierExpr: varName; -subscript: nonSubscriptExpr '[' expr ']'; +litExpr: INT_LIT | DECIMAL_LIT | STRING_LIT | BOOL_LIT; -memberAccess: identifier '.' identifier; +accessExpr: nonAccessExpr (accessSubExpr)+; +accessSubExpr: accessMember | accessBrackets; +accessMember: ('.' | '->') identifierExpr; +accessBrackets: '[' expr ']'; parenExpr: '(' expr ')'; @@ -86,11 +97,21 @@ typeName: NAME; structName: NAME; -BINARY_OPERATOR: - '+' | '-' | '*' | '/' - | '==' | '!=' - | '<' | '>'; -INTLIT: ('+' | '-')? [0-9]+; +postfix_op: + '++' | '--'; +prefix_op: + '+' | '-' | '!' | '~' | '&' | '*' | postfix_op; +binary_op: + '+' | '-' | '*' | '/' | '%' | '&' | '<' | '|' | '^' | '>' | + '==' | '!=' | '<=' | '>=' | '<' | '>' | + '<<' | '>>' | '||' | '&&' | '&=' | '|=' | '^=' | + '<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%='; + +INT_LIT: ('+' | '-')? [0-9]+; +DECIMAL_LIT: ('+' | '-')* [0-9]+ '.' [0-9]+; +STRING_LIT: '"' [^"]* '"'; +BOOL_LIT: 'true' | 'false'; + NAME: ([a-z] | [A-Z] | [0-9])+; WS: [ \t\r\n]+ -> skip; NEWLINE: [\r\n]+;