git @ Cat's Eye Technologies Decoy / 54df9aa
Basic support for strings and numbers. Chris Pressey 1 year, 7 months ago
5 changed file(s) with 36 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
117117 TODO
118118 ----
119119
120 * Support strings properly.
121 * Support numbers properly.
120 * Support floating-point numbers.
121 * Arithmetic operations.
122 * Comparison operators.
123 * `and`, `or`, `not`
122124 * Refine the "module system".
123125 * `import-from`
124126 * Start on the compiler framework.
125127 * `if`
128 * Evaluation of forms given on command line.
137137 error("Unbound symbol: " .. ast.text())
138138 end
139139 return value
140 elseif ast.class == String or ast.class == Number or ast.class == Boolean then
141 return ast
140142 else
141 error("not a Cons or Atom: " .. depict(ast))
143 error("Unsupported form for evaluation: " .. depict(ast))
142144 end
143145 end
8282 return module
8383 end
8484
85
8685 function process_toplevel(expr, env, module)
8786 if expr.class == Cons then
8887 if expr.head().class == Symbol then
101101 return
102102 end
103103
104 -- literal decimal number
105 if string:sub(1,1) == "-" or isdigit(string:sub(1,1)) then
106 local len = 0
107 if string:sub(1,1) == "-" then
108 len = len + 1
109 end
110 while isdigit(string:sub(1+len,1+len)) and len <= string:len() do
111 len = len + 1
112 end
113 fields.set_token(string:sub(1, len), "numlit")
114 string = string:sub(len + 1)
115 return
116 end
117
104118 -- quoted string
105119 if string:sub(1,1) == "\"" then
106120 local len = 1
114128 return
115129 end
116130
117 -- else check for symbols
131 -- symbol
118132 if issymbolic(string:sub(1,1)) then
119133 local len = 0
120134 while issymbolic(string:sub(1+len,1+len)) and len <= string:len() do
123137 local word = string:sub(1, 1+len-1)
124138 string = string:sub(1+len)
125139 fields.set_token(word, "symbol")
126 return
127 end
128
129 -- else check for literal decimal number
130 if string:sub(1,1) == "-" or isdigit(string:sub(1,1)) then
131 local len = 0
132 if string:sub(1,1) == "-" then
133 len = len + 1
134 end
135 while isdigit(string:sub(1+len,1+len)) and len <= string:len() do
136 len = len + 1
137 end
138 fields.set_token(string:sub(1, len), "numlit")
139 string = string:sub(len + 1)
140140 return
141141 end
142142
225225
226226 - - - -
227227
228 More Types
229 ----------
230
231 ### Strings
232
233 (let* ((a "hi") (b "there")) (list a b))
234 ===> ("hi" "there")
235
236 ### Numbers
237
238 (let* ((a 1) (b 2)) (list a b 3))
239 ===> (1 2 3)
240
228241 Builtins
229242 --------
230243