about summary refs log tree commit diff stats
path: root/parsing.lsp
blob: 3e26a35439c58b53a3404c6bc6fbf136e2057841 (plain) (blame)
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
35
36
37
38
39
40
41
42
43
(defpackage #:parsing
  (:use #:openlisp #:lex #:abs-syn)
  (:export
   #:parse))
(in-package #:parsing)

(defclass <exp-elem> () () (:abstractp t))
(defclass <elem-exp> (<exp-elem>) ((expr :accessor expr)))
(defclass <elem-bin> (<exp-elem>) ((bin-op :accessor bin-op)))
(defclass <elem-unr> (<exp-elem>) ((unr-op :accessor unr-op)))
(defclass <elem-lp> (<exp-elem>) ())

(defun unr-symb (s)
  (cond ((string= s "!") 'not)
	((string= s "-") 'uminus)
	(t (error "Parse error"))))

(defun bin-symb (s)
  (cond ((string= s "+") 'plus)
	((string= s "-") 'minus)
	((string= s "*") 'mult)
	((string= s "/") 'div)
	((string= s "%") 'mod)
	((string= s "=") 'equal)
	((string= s "<") 'less)
	((string= s "<=") 'lesseq)
	((string= s ">") 'great)))

(defun parse (str)
  (let* ((cl (init-lex str))
	 (tok (lexer cl)))
    (cond ((instancep tok (class <lint>))
	   (create (class <line>) 'n n 'c (parse-cmd cl)))
	  ((instancep tok (class <lident>))
	   (cond ((string= (ident tok) "LIST")
		  (create (class <phrase-list>)))
		 ((string= (ident tok) "RUN")
		  (create (class <phrase-run>)))
		 ((string= (ident tok) "END")
		  (create (class <phrase-p-end>)))
		 (t (error "Parse error"))))
	  (t (error "Parse error")))))
(provide "parsing")