lem 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/lem +18 -2
- data/lib/lem.rb +133 -3
- metadata +2 -2
data/bin/lem
CHANGED
@@ -2,5 +2,21 @@
|
|
2
2
|
|
3
3
|
require 'lem'
|
4
4
|
|
5
|
-
puts "
|
6
|
-
puts
|
5
|
+
puts "Starting Lem, a ruby implemenration of Scratch"
|
6
|
+
puts "Type \"exit\" to quit"
|
7
|
+
puts "----------------------------------------------"
|
8
|
+
puts ""
|
9
|
+
|
10
|
+
|
11
|
+
scr = Scratch.new
|
12
|
+
scr.add_words PrintingWords
|
13
|
+
scr.add_words MathWords
|
14
|
+
|
15
|
+
while true
|
16
|
+
print ">>> "
|
17
|
+
inp = gets
|
18
|
+
break if inp == "exit"
|
19
|
+
scr = Scratch.
|
20
|
+
end
|
21
|
+
|
22
|
+
puts "\nLem is awesome, huh?"
|
data/lib/lem.rb
CHANGED
@@ -1,5 +1,135 @@
|
|
1
|
-
class
|
2
|
-
def
|
3
|
-
|
1
|
+
class String
|
2
|
+
def is_num?
|
3
|
+
begin Float(self); true end rescue false
|
4
4
|
end
|
5
5
|
end
|
6
|
+
|
7
|
+
class ScratchLexer
|
8
|
+
def initialize(text)
|
9
|
+
@words = text.split(/\s+/)
|
10
|
+
@next = 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def next_word
|
14
|
+
return nil if @next >= @words.length
|
15
|
+
@next += 1
|
16
|
+
@words[@next-1]
|
17
|
+
end
|
18
|
+
|
19
|
+
def each
|
20
|
+
@words.each { |word| yield word }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Scratch
|
25
|
+
attr_reader :stack
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
@dictionary = {}
|
29
|
+
@stack = []
|
30
|
+
end
|
31
|
+
|
32
|
+
def add_words(dict)
|
33
|
+
dict.each do |k,v|
|
34
|
+
@dictionary[k] = v
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def run(text)
|
39
|
+
@lexer = ScratchLexer.new(text)
|
40
|
+
@lexer.each do |word|
|
41
|
+
word.upcase!
|
42
|
+
if word.is_num?
|
43
|
+
@stack.push word.to_f
|
44
|
+
elsif @dictionary[word]
|
45
|
+
@dictionary[word].call self
|
46
|
+
else
|
47
|
+
raise "Unknown word"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
PrintingWords = {
|
54
|
+
"PRINT" => Proc.new do |terp|
|
55
|
+
raise "Not enough items on the stack" if terp.stack.length < 1
|
56
|
+
tos = terp.stack.pop
|
57
|
+
puts tos
|
58
|
+
end,
|
59
|
+
|
60
|
+
"PSTACK" => Proc.new do |terp|
|
61
|
+
puts terp.stack
|
62
|
+
end
|
63
|
+
}
|
64
|
+
|
65
|
+
MathWords = {
|
66
|
+
"+" => Proc.new do |terp|
|
67
|
+
raise "Not enough items on the stack" if terp.stack.length < 2
|
68
|
+
tos = terp.stack.pop
|
69
|
+
tos2 = terp.stack.pop
|
70
|
+
terp.stack.push tos+tos2
|
71
|
+
end,
|
72
|
+
"-" => Proc.new do |terp|
|
73
|
+
raise "Not enough items on the stack" if terp.stack.length < 2
|
74
|
+
tos = terp.stack.pop
|
75
|
+
tos2 = terp.stack.pop
|
76
|
+
terp.stack.push tos-tos2
|
77
|
+
end,
|
78
|
+
"*" => Proc.new do |terp|
|
79
|
+
raise "Not enough items on the stack" if terp.stack.length < 2
|
80
|
+
tos = terp.stack.pop
|
81
|
+
tos2 = terp.stack.pop
|
82
|
+
terp.stack.push tos*tos2
|
83
|
+
end,
|
84
|
+
"/" => Proc.new do |terp|
|
85
|
+
raise "Not enough items on the stack" if terp.stack.length < 2
|
86
|
+
tos = terp.stack.pop
|
87
|
+
tos2 = terp.stack.pop
|
88
|
+
terp.stack.push tos/tos2
|
89
|
+
end,
|
90
|
+
"SQRT" => Proc.new do |terp|
|
91
|
+
raise "Not enough items on the stack" if terp.stack.length < 1
|
92
|
+
tos = terp.stack.pop
|
93
|
+
terp.stack.push Math.sqrt(tos)
|
94
|
+
end
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
date: 2011-10-19 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
|
-
description:
|
14
|
+
description: Scratch Programming Language
|
15
15
|
email: lord.lem.l@gmail.com
|
16
16
|
executables:
|
17
17
|
- lem
|