diff options
Diffstat (limited to 'src/modules/parser')
-rw-r--r-- | src/modules/parser/parser.scm | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/modules/parser/parser.scm b/src/modules/parser/parser.scm index b2ca12f..e70a825 100644 --- a/src/modules/parser/parser.scm +++ b/src/modules/parser/parser.scm @@ -1 +1,34 @@ -(define-module (modules parser parser)) +(define-module (modules parser parser) + #:use-module (ice-9 match) + #:export (p-program)) + + +(define (die) + (error "syntax error")) + + +(define (p-program tokens) + (match tokens + ((func ...) + `(program ,(p-function func))) + (_ (die)))) + +(define (p-function tokens) + (match tokens + (('int (? string? id) 'left-paren 'void 'right-paren 'open-brace stmt ... 'close-brace) + `(function (identifier ,id) ,(p-statement stmt))) + (_ (die)))) + +(define (p-statement tokens) + (match tokens + (`(return ,expr semi-colon) + `(return ,(p-exp (list expr)))) + (_ (die)))) + +(define (p-exp tokens) + "Matches any list containing a single number, +" + (match tokens + (((? number? const)) + `(constant ,const)) + (_ (die)))) |