-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.lisp
executable file
·101 lines (92 loc) · 2.32 KB
/
parser.lisp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
;#!/usr/local/bin/sbcl --noinform
;#!/sw/bin/clisp
;#!/usr/local/bin/clisp
;;;; Hey, Emacs, this is a -*- Mode: Lisp; Syntax: Common-Lisp -*- file!
;;;;
;;;; Programming should be fun. Programs should be beautiful.
;;;; -- Paul Graham
;;;;
;;;; Name: parser.lisp
;;;;
;;;; Started: Thu Dec 22 23:02:55 2005
;;;; Modifications:
;;;;
;;;; Purpose:
;;;;
;;;;
;;;;
;;;; Calling Sequence:
;;;;
;;;;
;;;; Inputs:
;;;;
;;;; Outputs:
;;;;
;;;; Example:
;;;;
;;;; Notes: Recursive descent parser (Art of Java)
;;;;
;;;; What about assignment (=) ?
;;;;
;;;;
(defpackage parser (:use common-lisp tokenizer))
(in-package parser)
(let (token-list token)
(defun infix->prefix (s)
(tokenize s)
(setf token-list (get-tokens))
(setf token (pop token-list))
(let ((result (eval-2)))
result))
(defun eval-2 ()
(let ((result (eval-3)))
(loop (unless (or (equal token '+)
(equal token '-))
(return result))
(let ((operator token))
(setf token (pop token-list))
(let ((partial-result (eval-3)))
(setf result (list operator result partial-result)))) )))
(defun eval-3 ()
(let ((result (eval-4)))
(loop (unless (or (equal token '*)
(equal token '/)
(equal token '%))
(return result))
(let ((operator token))
(setf token (pop token-list))
(let ((partial-result (eval-4)))
(setf result (list operator result partial-result)))) )))
(defun eval-4 ()
(let ((result (eval-5)))
(cond ((equal token '^)
(let ((operator token)) ; Don't need to keep
(setf token (pop token-list))
(let ((partial-result (eval-4)))
(setf result (list 'expt result partial-result)))) )
(t result))))
(defun eval-5 ()
"Handle unary +/-"
(case token
(+ (setf token (pop token-list))
(eval-6))
(- (setf token (pop token-list))
(list '- (eval-6)))
(otherwise (eval-6))))
(defun eval-6 ()
(cond ((equal token '|(|)
(setf token (pop token-list))
(let ((result (eval-2)))
(setf token (pop token-list))
result))
(t (eval-atom))))
;;;
;;; Atomic
;;;
;;; Must handle number/symbol (Look up value of symbol)
;;;
(defun eval-atom ()
; (print token)
(let ((current-token token))
(setf token (pop token-list))
current-token)))