Skip to content

Commit a08aa10

Browse files
author
oxe-i
committed
add forth
1 parent eeca461 commit a08aa10

File tree

13 files changed

+756
-31
lines changed

13 files changed

+756
-31
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,14 @@
456456
"practices": [],
457457
"prerequisites": [],
458458
"difficulty": 8
459+
},
460+
{
461+
"slug": "forth",
462+
"name": "Forth",
463+
"uuid": "a9f94a1f-2ac6-4792-b6e7-d2ea0d65549c",
464+
"practices": [],
465+
"prerequisites": [],
466+
"difficulty": 9
459467
}
460468
]
461469
},
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Instructions
2+
3+
Implement an evaluator for a very simple subset of Forth.
4+
5+
[Forth][forth]
6+
is a stack-based programming language.
7+
Implement a very basic evaluator for a small subset of Forth.
8+
9+
Your evaluator has to support the following words:
10+
11+
- `+`, `-`, `*`, `/` (integer arithmetic)
12+
- `DUP`, `DROP`, `SWAP`, `OVER` (stack manipulation)
13+
14+
Your evaluator also has to support defining new words using the customary syntax: `: word-name definition ;`.
15+
16+
To keep things simple the only data type you need to support is signed integers of at least 16 bits size.
17+
18+
You should use the following rules for the syntax: a number is a sequence of one or more (ASCII) digits, a word is a sequence of one or more letters, digits, symbols or punctuation that is not a number.
19+
(Forth probably uses slightly different rules, but this is close enough.)
20+
21+
Words are case-insensitive.
22+
23+
[forth]: https://en.wikipedia.org/wiki/Forth_%28programming_language%29
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import Std
2+
3+
namespace Forth
4+
5+
abbrev Stack := List Int
6+
abbrev Op := Stack -> Except String Stack
7+
abbrev OpMap := Std.TreeMap String Op
8+
9+
structure State where
10+
stack : Stack
11+
opMap : OpMap
12+
13+
def applyUnary (op : Int -> Except String Stack) (stack : Stack) : Except String Stack :=
14+
match stack with
15+
| [] => .error "empty stack"
16+
| x :: xs => op x >>= fun rs => .ok (rs ++ xs)
17+
18+
def applyBinary (op : Int -> Int -> Except String Stack) (stack : Stack) : Except String Stack :=
19+
match stack with
20+
| [] => .error "empty stack"
21+
| _ :: [] => .error "only one value on the stack"
22+
| x1 :: x2 :: xs => op x2 x1 >>= fun rs => .ok (rs ++ xs)
23+
24+
def parseExpression (state : State) (expression : String) : Except String State :=
25+
match expression.toInt? with
26+
| some value => .ok { state with stack := (value :: state.stack) } -- if number, adds it to the stack
27+
| none => match state.opMap.get? expression with
28+
| some op => op state.stack >>= (.ok { state with stack := · }) -- applies op, gets the resulting stack and replaces state stack with it
29+
| none => .error "undefined operation"
30+
31+
def checkOp (op : String) (opMap : OpMap) : Except String Op :=
32+
match opMap.get? op with
33+
| some op => .ok op -- if op is a key already in map, return its op -> this allows for indirect ops that reference previously defined user-ops
34+
| none => .ok (fun stack => parseExpression { stack := stack, opMap := opMap } op >>= fun state => .ok state.stack) -- otherwise, an op is nothing more than an expression to parse
35+
36+
def addUserOp (pattern : String) (state : State) : Except String Op :=
37+
let exceptOps := pattern.splitOn " " |> (·.mapM (checkOp · state.opMap))
38+
exceptOps >>= fun ops => .ok (ops.foldlM (fun acc op => op acc))
39+
40+
def parseInstruction (state : State) (instruction : String) : Except String State :=
41+
let expressions := instruction.toLower.splitOn " "
42+
match expressions with
43+
| [] => .ok state
44+
| ":" :: key :: xs =>
45+
match key.toInt? with
46+
| some _ => .error "illegal operation" -- number can't be an user-defined op
47+
| none =>
48+
let userOp := String.intercalate " " xs.dropLast -- drops ; at the end
49+
addUserOp userOp state >>= fun op => .ok { state with opMap := state.opMap.insert key op } -- adds op to map, possibly replacing previous op with same key
50+
| _ => expressions.foldlM parseExpression state -- parses expressions, short-circuiting on error
51+
52+
def evaluate (instructions : List String) : Except String Stack :=
53+
let initial : State := {
54+
stack := [],
55+
opMap := .ofList [
56+
-- in all binary ops, b is the top of the stack and a is further inside
57+
("+", applyBinary (fun a b => .ok [a + b])),
58+
("-", applyBinary (fun a b => .ok [a - b])),
59+
("*", applyBinary (fun a b => .ok [a * b])),
60+
("/", applyBinary (fun a b => if b == 0 then .error "divide by zero" else .ok [a / b])),
61+
("dup", applyUnary (fun a => .ok [a, a])),
62+
("drop", applyUnary (fun a => .ok [])),
63+
("swap", applyBinary (fun a b => .ok [a, b])),
64+
("over", applyBinary (fun a b => .ok [a, b, a]))
65+
]
66+
}
67+
68+
instructions.foldlM parseInstruction initial -- monadic fold, short-circuits on error
69+
>>= fun state => .ok state.stack.reverse -- if successful, returns stack reversed lifted to Except
70+
71+
end Forth
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"oxe-i"
4+
],
5+
"files": {
6+
"solution": [
7+
"Forth.lean"
8+
],
9+
"test": [
10+
"ForthTest.lean"
11+
],
12+
"example": [
13+
".meta/Example.lean"
14+
]
15+
},
16+
"blurb": "Implement an evaluator for a very simple subset of Forth."
17+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[9962203f-f00a-4a85-b404-8a8ecbcec09d]
13+
description = "parsing and numbers -> numbers just get pushed onto the stack"
14+
15+
[fd7a8da2-6818-4203-a866-fed0714e7aa0]
16+
description = "parsing and numbers -> pushes negative numbers onto the stack"
17+
18+
[9e69588e-a3d8-41a3-a371-ea02206c1e6e]
19+
description = "addition -> can add two numbers"
20+
21+
[52336dd3-30da-4e5c-8523-bdf9a3427657]
22+
description = "addition -> errors if there is nothing on the stack"
23+
24+
[06efb9a4-817a-435e-b509-06166993c1b8]
25+
description = "addition -> errors if there is only one value on the stack"
26+
27+
[1e07a098-c5fa-4c66-97b2-3c81205dbc2f]
28+
description = "addition -> more than two values on the stack"
29+
30+
[09687c99-7bbc-44af-8526-e402f997ccbf]
31+
description = "subtraction -> can subtract two numbers"
32+
33+
[5d63eee2-1f7d-4538-b475-e27682ab8032]
34+
description = "subtraction -> errors if there is nothing on the stack"
35+
36+
[b3cee1b2-9159-418a-b00d-a1bb3765c23b]
37+
description = "subtraction -> errors if there is only one value on the stack"
38+
39+
[2c8cc5ed-da97-4cb1-8b98-fa7b526644f4]
40+
description = "subtraction -> more than two values on the stack"
41+
42+
[5df0ceb5-922e-401f-974d-8287427dbf21]
43+
description = "multiplication -> can multiply two numbers"
44+
45+
[9e004339-15ac-4063-8ec1-5720f4e75046]
46+
description = "multiplication -> errors if there is nothing on the stack"
47+
48+
[8ba4b432-9f94-41e0-8fae-3b3712bd51b3]
49+
description = "multiplication -> errors if there is only one value on the stack"
50+
51+
[5cd085b5-deb1-43cc-9c17-6b1c38bc9970]
52+
description = "multiplication -> more than two values on the stack"
53+
54+
[e74c2204-b057-4cff-9aa9-31c7c97a93f5]
55+
description = "division -> can divide two numbers"
56+
57+
[54f6711c-4b14-4bb0-98ad-d974a22c4620]
58+
description = "division -> performs integer division"
59+
60+
[a5df3219-29b4-4d2f-b427-81f82f42a3f1]
61+
description = "division -> errors if dividing by zero"
62+
63+
[1d5bb6b3-6749-4e02-8a79-b5d4d334cb8a]
64+
description = "division -> errors if there is nothing on the stack"
65+
66+
[d5547f43-c2ff-4d5c-9cb0-2a4f6684c20d]
67+
description = "division -> errors if there is only one value on the stack"
68+
69+
[f224f3e0-b6b6-4864-81de-9769ecefa03f]
70+
description = "division -> more than two values on the stack"
71+
72+
[ee28d729-6692-4a30-b9be-0d830c52a68c]
73+
description = "combined arithmetic -> addition and subtraction"
74+
75+
[40b197da-fa4b-4aca-a50b-f000d19422c1]
76+
description = "combined arithmetic -> multiplication and division"
77+
78+
[f749b540-53aa-458e-87ec-a70797eddbcb]
79+
description = "combined arithmetic -> multiplication and addition"
80+
81+
[c8e5a4c2-f9bf-4805-9a35-3c3314e4989a]
82+
description = "combined arithmetic -> addition and multiplication"
83+
84+
[c5758235-6eef-4bf6-ab62-c878e50b9957]
85+
description = "dup -> copies a value on the stack"
86+
87+
[f6889006-5a40-41e7-beb3-43b09e5a22f4]
88+
description = "dup -> copies the top value on the stack"
89+
90+
[40b7569c-8401-4bd4-a30d-9adf70d11bc4]
91+
description = "dup -> errors if there is nothing on the stack"
92+
93+
[1971da68-1df2-4569-927a-72bf5bb7263c]
94+
description = "drop -> removes the top value on the stack if it is the only one"
95+
96+
[8929d9f2-4a78-4e0f-90ad-be1a0f313fd9]
97+
description = "drop -> removes the top value on the stack if it is not the only one"
98+
99+
[6dd31873-6dd7-4cb8-9e90-7daa33ba045c]
100+
description = "drop -> errors if there is nothing on the stack"
101+
102+
[3ee68e62-f98a-4cce-9e6c-8aae6c65a4e3]
103+
description = "swap -> swaps the top two values on the stack if they are the only ones"
104+
105+
[8ce869d5-a503-44e4-ab55-1da36816ff1c]
106+
description = "swap -> swaps the top two values on the stack if they are not the only ones"
107+
108+
[74ba5b2a-b028-4759-9176-c5c0e7b2b154]
109+
description = "swap -> errors if there is nothing on the stack"
110+
111+
[dd52e154-5d0d-4a5c-9e5d-73eb36052bc8]
112+
description = "swap -> errors if there is only one value on the stack"
113+
114+
[a2654074-ba68-4f93-b014-6b12693a8b50]
115+
description = "over -> copies the second element if there are only two"
116+
117+
[c5b51097-741a-4da7-8736-5c93fa856339]
118+
description = "over -> copies the second element if there are more than two"
119+
120+
[6e1703a6-5963-4a03-abba-02e77e3181fd]
121+
description = "over -> errors if there is nothing on the stack"
122+
123+
[ee574dc4-ef71-46f6-8c6a-b4af3a10c45f]
124+
description = "over -> errors if there is only one value on the stack"
125+
126+
[ed45cbbf-4dbf-4901-825b-54b20dbee53b]
127+
description = "user-defined words -> can consist of built-in words"
128+
129+
[2726ea44-73e4-436b-bc2b-5ff0c6aa014b]
130+
description = "user-defined words -> execute in the right order"
131+
132+
[9e53c2d0-b8ef-4ad8-b2c9-a559b421eb33]
133+
description = "user-defined words -> can override other user-defined words"
134+
135+
[669db3f3-5bd6-4be0-83d1-618cd6e4984b]
136+
description = "user-defined words -> can override built-in words"
137+
138+
[588de2f0-c56e-4c68-be0b-0bb1e603c500]
139+
description = "user-defined words -> can override built-in operators"
140+
141+
[ac12aaaf-26c6-4a10-8b3c-1c958fa2914c]
142+
description = "user-defined words -> can use different words with the same name"
143+
144+
[53f82ef0-2750-4ccb-ac04-5d8c1aefabb1]
145+
description = "user-defined words -> can define word that uses word with the same name"
146+
147+
[35958cee-a976-4a0f-9378-f678518fa322]
148+
description = "user-defined words -> cannot redefine non-negative numbers"
149+
150+
[df5b2815-3843-4f55-b16c-c3ed507292a7]
151+
description = "user-defined words -> cannot redefine negative numbers"
152+
153+
[5180f261-89dd-491e-b230-62737e09806f]
154+
description = "user-defined words -> errors if executing a non-existent word"
155+
156+
[3c8bfef3-edbb-49c1-9993-21d4030043cb]
157+
description = "user-defined words -> only defines locally"
158+
include = false
159+
160+
[7b83bb2e-b0e8-461f-ad3b-96ee2e111ed6]
161+
description = "case-insensitivity -> DUP is case-insensitive"
162+
163+
[339ed30b-f5b4-47ff-ab1c-67591a9cd336]
164+
description = "case-insensitivity -> DROP is case-insensitive"
165+
166+
[ee1af31e-1355-4b1b-bb95-f9d0b2961b87]
167+
description = "case-insensitivity -> SWAP is case-insensitive"
168+
169+
[acdc3a49-14c8-4cc2-945d-11edee6408fa]
170+
description = "case-insensitivity -> OVER is case-insensitive"
171+
172+
[5934454f-a24f-4efc-9fdd-5794e5f0c23c]
173+
description = "case-insensitivity -> user-defined words are case-insensitive"
174+
175+
[037d4299-195f-4be7-a46d-f07ca6280a06]
176+
description = "case-insensitivity -> definitions are case-insensitive"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Forth
2+
3+
def evaluate (instructions : List String) : Except String (List Int) :=
4+
sorry
5+
6+
end Forth

0 commit comments

Comments
 (0)