Browse Source

Add function calls, field access operators

pull/1/head
Max Brunsfeld 12 years ago
parent
commit
fae81ec733
3 changed files with 65 additions and 1 deletions
  1. +18
    -1
      grammar.coffee
  2. +30
    -0
      grammar_test/expressions.txt
  3. +17
    -0
      grammar_test/statements.txt

+ 18
- 1
grammar.coffee View File

@ -67,11 +67,15 @@ module.exports = compiler.grammar
"{", err(repeat(@statement)), "}")
statement: -> choice(
@return_statement),
@return_statement,
@expression_statement),
return_statement: -> seq(
keyword("return"), @expression, ";")
expression_statement: -> seq(
@expression, ";")
type: -> seq(
optional(keyword("const")),
choice(
@ -141,9 +145,22 @@ module.exports = compiler.grammar
keyword("const"))
expression: -> choice(
@identifier,
@function_call,
@field_access,
@deref_field_access,
@number,
@string)
field_access: -> seq(
@expression, ".", @identifier)
deref_field_access: -> seq(
@expression, "->", @identifier)
function_call: -> seq(
@expression, "(", err(commaSep(@expression)), ")")
number: -> /\d+(\.\d+)?/
string: -> token(seq(

+ 30
- 0
grammar_test/expressions.txt View File

@ -0,0 +1,30 @@
============================================
Function calls
============================================
int main() {
printf("hi! %d\n", x);
}
---
(function_declaration (identifier) (identifier) (formal_parameters)
(statement_block
(expression_statement (function_call (identifier) (string) (identifier)))))
============================================
Field access
============================================
int main() {
s.data1;
p->data2;
}
---
(function_declaration (identifier) (identifier) (formal_parameters)
(statement_block
(expression_statement (field_access (identifier) (identifier)))
(expression_statement (deref_field_access (identifier) (identifier)))))

+ 17
- 0
grammar_test/statements.txt View File

@ -0,0 +1,17 @@
============================================
Expression statements
============================================
int main() {
x;
}
---
(function_declaration
(identifier)
(identifier)
(formal_parameters)
(statement_block
(expression_statement (identifier))))

Loading…
Cancel
Save