-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminiforth.moon
More file actions
46 lines (37 loc) · 1.14 KB
/
miniforth.moon
File metadata and controls
46 lines (37 loc) · 1.14 KB
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
import extend from require "moon"
class MiniForth
new: (subj = "") =>
@subject = subj
@position = 1
@dictionary = {}
@mode = "interpret"
@modes = {interpret: ->
@word = @get_word_or_newline! or ""
@p_s_i!
@interpret_primitive! or @interpret_nonprimitive! or @interpret_number! or error string.format([[Can't interpret: "%s"]], @word)
}
add_words: (words) => @dictionary = extend(words, @dictionary)
parse_by_pattern: (pat) =>
cap, newpos = string.match @subject, pat, @position
if newpos
@position = newpos
cap
parse_spaces: => @parse_by_pattern "^([ \t]*)()"
parse_word: => @parse_by_pattern "^(%S+)()"
parse_newline: => @parse_by_pattern "^(\n)()"
parse_rest_of_line: => @parse_by_pattern "^([^\n]*)()"
parse_word_or_newline: => @parse_word! or @parse_newline!
get_word: =>
@parse_spaces!
@parse_word!
get_word_or_newline: =>
@parse_spaces!
@parse_word_or_newline!
interpret_primitive: => if type(@dictionary[@word]) == "function" then
@dictionary[@word]!
true
interpret_nonprimitive: => false
interpret_number: => false
p_s_i: =>
run: => while @mode != "stop" do @modes[@mode]!
{ :MiniForth }