chiark / gitweb /
Work in progress. Mostly bug fixing.
[sod] / src / pset-parse.lisp
CommitLineData
bf090e02
MW
1;;; -*-lisp-*-
2;;;
3;;; Parsing property sets
4;;;
5;;; (c) 2012 Straylight/Edgeware
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
10;;; This file is part of the Sensble Object Design, an object system for C.
11;;;
12;;; SOD is free software; you can redistribute it and/or modify
13;;; it under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 2 of the License, or
15;;; (at your option) any later version.
16;;;
17;;; SOD is distributed in the hope that it will be useful,
18;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with SOD; if not, write to the Free Software Foundation,
24;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
239fa5bd
MW
26;;;--------------------------------------------------------------------------
27;;; The expression parser.
28
29(flet ((dispatch (name args &rest spec)
30 (acond ((find :invalid args :key #'car)
31 (cons :invalid nil))
32 ((find-if (lambda (item)
33 (every (lambda (type arg)
34 (eql type (car arg)))
35 (cddr item)
36 args))
37 spec)
38 (cons (car it) (apply (cadr it)
39 (mapcar #'cdr args))))
40 (t
41 (cerror* "Type mismatch: operator `~A' applied to ~
42 types ~{~(~A~)~#[~; and ~;, ~]~}"
43 name
44 (mapcar #'car args))
45 (cons :invalid nil)))))
46 (let ((add (binop "+" (x y 5)
47 (dispatch "+" (list x y) (list :int #'+ :int :int))))
48 (sub (binop "-" (x y 5)
49 (dispatch "-" (list x y) (list :int #'- :int :int))))
50 (mul (binop "*" (x y 7)
51 (dispatch "*" (list x y) (list :int #'* :int :int))))
52 (div (binop "/" (x y 7)
53 (dispatch "/" (list x y)
54 (list :int
55 (lambda (x y)
56 (cond ((zerop y)
57 (cerror*
58 "Division by zero")
59 (cons :invalid nil))
60 (t
61 (floor x y))))
62 :int :int))))
63 (nop (preop "+" (x 9)
64 (dispatch "+" (list x) (list :int #'+ :int))))
65 (neg (preop "-" (x 9)
66 (dispatch "-" (list x) (list :int #'- :int))))
67 (lp (lparen #\)))
68 (rp (rparen #\))))
69
70 (defun parse-expression (scanner)
71 "Parse and evaluate a simple expression.
bf090e02
MW
72
73 The result is a pair (TYPE . VALUE). Currently, type types are `:id',
239fa5bd 74 `:int', `:string', and `:char'. If an error prevented a sane value from
bf090e02
MW
75 being produced, the type `:invalid' is returned.
76
77 The syntax of expressions is rather limited at the moment, but more may be
78 added later.
79
80 expression: term | expression `+' term | expression `-' term
81 term: factor | term `*' factor | term `/' factor
82 factor: primary | `+' factor | `-' factor
83 primary: int | id | string | `(' expression `)' | `?' lisp-expression
84
85 Only operators for dealing with integers are provided."
239fa5bd
MW
86 (with-parser-context (token-scanner-context :scanner scanner)
87 (parse (expr (:nestedp nestedp)
88 (lisp (flet ((prop (type value)
bf090e02 89 (scanner-step scanner)
239fa5bd
MW
90 (values (cons type value) t t)))
91 (case (token-type scanner)
92 ((:int :id :char :string)
93 (prop (token-type scanner)
94 (token-value scanner)))
95 (#\?
96 (let* ((stream (make-scanner-stream scanner))
97 (sexp (read stream t)))
98 (scanner-step scanner)
99 (values (cons (property-type sexp) sexp)
100 t t)))
101 (t
102 (values (list :int :id :char :string #\?)
103 nil nil)))))
104 (or (seq (#\+) add)
105 (seq (#\-) sub)
106 (seq (#\*) mul)
107 (seq (#\/) div))
108 (or (seq (#\() lp)
109 (seq (#\+) nop)
110 (seq (#\-) neg))
111 (when nestedp (seq (#\)) rp))))))))
112
113;;;--------------------------------------------------------------------------
114;;; Parsing property sets.
bf090e02
MW
115
116(defun parse-property (scanner pset)
117 "Parse a single property using the SCANNER; add it to the PSET."
239fa5bd
MW
118 ;; property ::= id `=' expression
119 (with-parser-context (token-scanner-context :scanner scanner)
120 (parse (seq ((name :id) #\= (result (parse-expression scanner)))
121 (let ((type (car result))
122 (value (cdr result)))
123 (unless (eq type :invalid)
124 (add-property pset name value
125 :type type
126 :location scanner)))))))
127
128(export 'parse-property-set)
129(defun parse-property-set (scanner)
130 "Parse an optional property set from the SCANNER and return it, or `nil'."
131 ;; property-set ::= [`[' property-list `]']
132 (with-parser-context (token-scanner-context :scanner scanner)
133 (parse (? (seq (#\[
134 (pset (many (pset (make-property-set) pset)
135 (parse-property scanner pset)
136 #\,))
137 #\])
138 pset)))))
bf090e02
MW
139
140;;;----- That's all, folks --------------------------------------------------