-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.txt
119 lines (81 loc) · 1.97 KB
/
task.txt
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
The goal of this challenge is to prove your coding capabilities,
platform-specific knowledge are not required at all, spending a few
hours. If you do not complete the task, please submit it whatever you
did, clarifying out much time you spent on it.
* application: command line is perfectly fine. Implement UI if it is
quicker for you
* platform: windows or macOS
* programming language: C++ or Objective C. Swift not accepted
Implement a very basic evaluator for a small subset of Forth. which is a
stack-based programming language.
Your evaluator has to support the following words:
+, -, , / (integer arithmetic)
* DUP, DROP, SWAP, OVER (stack manipulation)
[OPTIONAL] Your evaluator also has to support defining new words using
the customary
syntax: : word-name definition ;.
To keep things simple the only data type you need to support is signed
integers of at least 16 bits size.
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. (Forth probably uses
slightly different rules, but this is close enough.)
Words are case-insensitive.
What will be evaluated in the code:
- Memory management
- Data structures
Some test cases to guide the development:
=> 1 2 3 4 5
Expected stack: 1 2 3 4 5
=> 1 2 +
Expected stack: 3
=> +
Error
=> 1 +
Error
=> 3 4 -
-1
=> -
Error
=> 2 4 *
8
=> 12 3 /
4
=> 1 2 + 4 -
-1
=> 2 4 * 3 /
2
=> 1 dup
1 1
=> 1 2 dup
1 2 2
=> dup
error
=> 1 2 drop
1
=> drop
error
=> 1 2 swap
2 1
=> 1 2 3 SWAP
1 3 2
=> swap
error
=> 1 2 over
1 2 1
=> 1 2 3 Over
1 2 3 2
=> over
error
=> : dup-twice dup dup ; 1 dup-twice
1, 1, 1
=> : countup 1 2 3 ; countup
1 2 3
=> : foo dup ; : foo dup dup ; 1 foo
1 1 1
=> : swap dup ; 1 swap
1 1
=> : + * ; 3 4 +
12
=> : SWAP DUP Dup dup ; 1 Swap
1 1 1 1