summaryrefslogtreecommitdiff
path: root/src/modules/parser/parser.scm
blob: e70a825dd49579ab7a54988dee411cd04d7aff83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(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))))