blob: e47e1b738bb916bdd56b051e0ca97cab430befb2 (
plain)
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
|
# dumb-stack-lang
This is a tinkering repo for an interpreted stack-based language implemented in Haskell.
God help you if you want to use this for anything.
## Current features
### Datatypes
* Booleans
* Arbitrary-prescision integers
* Strings
### Procedures
Code is separated into procedures. A program must define a procedure named `MAIN` to serve as the entrypoint.
```
PROC is-even? 2 OVER % 0 == END
PROC MAIN
4 is-even? . '''true'''
5 is-even? . '''false'''
END
```
### Postfix/Reverse Polish notation
Procedures are invoked by pushing its arguments to the stack in reverse order and then calling the procedure:
```
PROC
''' compute 2+1 and print the result (3) '''
1 2 + .
''' compute 2-1 and print the result (1) '''
1 2 - .
''' compute 1-2 and print the result (-1) '''
2 1 - .
''' test if 5 is greater than 6 and print the result (false) '''
6 5 > .
''' compute 2+3, test if 3 is less than the sum, and print the result '''
3 2 + 3 < .
''' concatenate two strings and print the result '''
" but backwards" "it's like LISP" ++ .
END
```
### Comments
Comments are multi-line only and are opened and closed with three single-quotes:
```
PROC MAIN
IF 2 9 % 0 == DO
''' TODO: come up with a better message to print
maybe something to do with the mod result being 0? '''
"true branch"
ELSE
''' i feel like we could mention that the number is odd? or something? '''
"false branch"
END .
END
```
### Control-flow
DSL currently has IF/ELIF/ELSE blocks and WHILE loops:
```
PROC MAIN
IF get-some-number 0 == DO
"the number is zero" .
ELIF get-some-number 1 == DO
"the number is one" .
ELIF get-some-number 2 == DO
"the number is two" .
ELSE
"this number has yet to be discovered by science" .
END
END
```
### Special characters
Most characters typically thought-of as 'special' have no special syntactic meaning in DSL. Some exceptions include:
* Sequences of characters that would be parsed as data literals
* Double-quotes, which start and end string literals
* Three single-quotes in a row, which start and end comment blocks
* Other special character sequences already reserved as intrinsic / proc names
```
''' these are okay '''
PROC foo "this proc has a valid name" . END
PROC +foo- "this proc has a valid name" . END
PROC +fo8o- "this proc has a valid name" . END
PROC $(*^$%*&#$%#]} "this proc has a valid name" . END
''' these aren't '''
PROC true "this is a parser error" . END
PROC 123 "this is a parser error" . END
PROC "foo" "this is a parser error" . END
PROC "foo "this is a parser error" . END
PROC '''foo''' "this is a parser error" . END
```
|