summaryrefslogtreecommitdiff
path: root/base/lang-process.md
blob: 1175fcaa1c1bd534f768ab4e01387ba3904e7742 (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
# Top level reader loop
First, a command is read in from the user under a prompt

Next, it is checked if the first token of the command (split on " ")
corresponds to an action name
* If it does, the corresponding action is executed, and the next command is
  read
* If it doesn't, the command is parsed as a language command and executed

This continues until the command "quit" (non case-sensitive) is read

# Known actions
The currently implemented actions are:
* env: print out the contents of the enviroment. Takes no arguments
* inline: inline variables out of the definition of specified variables. Takes
  at least two arguments
  * The first is the name of the variable to do inlining in
  * The second is the name of the variable to bind the inlined expression to
  * The third and following are the names of the variables to inline in the
	variable you are inlining. If you don't give any, it will inline every
	variable

## Details on inlining
The way the inlining process works is simple. The tree for the variable to be
inlined is read, and then each variable reference is inlined if it is marked as
one of the variable references to inline. This only occurs one layer deep

# Parsing language commands
Once it is decided to parse a command as a language command, it goes through
four steps:
* Preparation
* AST Building
* AST Transformation
* AST Evaluation

# Command preparation
Command preparation means turning the raw space-seperated tokens into tokens
suitable for feeding the AST builder. It involves the following steps:
* The first is operator expansion, which turns a token like 4+4 into the three
  tokens 4, +, and 4
* The next is token deaffixation which turns a token like (4 into the tokens (
  and 4. However, ((4 will become (( 4, not ( ( 4, because (( is a different
  nesting level than (
* Next, any blank tokens that have been created as a result of the process are
  disposed of
* Finally, the entire expression is shunted, turning it from infix notation to
  postfix notation. The only real special thing about this is that (( is a
  different nesting level than (. The token precedence levels are (sorted from
  lower to higher precedence): 
  * Math precedence
   * + & -
   * * & /
   * 
  * Dice precedence
   * d
   * c
  * Expression precedence
   * =>
   * :=

# AST Building
AST building is a slightly more complex process, as the tree is traversed
twice: once to build the tree, and then again to convert the tokens from
strings into data tokens. The only really finicky part about the first tree is
arrays, because arrays are still in infix form at this point.