git @ Cat's Eye Technologies UampirNexol / 7534294
Parsing and testing of `routine` and `call`. Chris Pressey 2 months ago
2 changed file(s) with 43 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
5959
6060 let foo = 0 in LDA# foo
6161 ===> Bind "foo" (IntLit 0) (Apply LDA_i (VarRef "foo"))
62
63 Syntax of `routine` and `call`, including typical usage in `let`, and precedence.
64
65 call routine (LDA# 0; TAX)
66 ===> Call (Routine (Seq (Apply LDA_i (IntLit 0)) TAX))
67
68 call (routine LDA# 0); TAX
69 ===> Call (Seq (Routine (Apply LDA_i (IntLit 0))) TAX)
70
71 let foo = routine (LDA# 0; TAX) in call foo
72 ===> Bind "foo" (Routine (Seq (Apply LDA_i (IntLit 0)) TAX)) (Call (VarRef "foo"))
6273
6374 Note the precedence of `;` is higher than that of `while`.
6475
217228 let foo = INX in (LDA# foo ; TAX)
218229 ???> error: Unacceptable argument type
219230
231 Type of routines.
232
233 routine (LDA# 0; TAX)
234 ===> Routine(State[/] -> State[A,X,Z,N/])
235
236 call routine (LDA# 0; TAX)
237 ===> (State[/] -> State[A,X,Z,N/])
238
220239 Code Extraction
221240 ---------------
222241
99 -- Expr1 ::= Expr2 [Expr1].
1010 -- Expr2 ::= Instruction
1111 -- | IntLit
12 -- | Location -- TODO
13 -- | VarName -- TODO
12 -- | Location
13 -- | VarName
14 -- | "routine" Expr
15 -- | "call" Expr
16 -- | "let" {VarName "=" Expr} "in" Expr
1417 -- | "if" Condition Expr "else" Expr
1518 -- | "repeat" Expr "until" Condition
1619 -- | "while" Condition "do" Expr
17 -- | "let" {VarName "=" Expr} "in" Expr
1820 -- | "(" Expr ")".
1921 -- Instruction ::= "LDA#" | "CLC" | etc.
2022 -- Condition ::= Flag | "~" Flag.
4143 Token.reservedNames =
4244 [
4345 "LDA#", "TAX", "TAY", "CLC", "INX", "INY",
46 "let", "in", "routine", "call",
4447 "if", "else", "repeat", "until", "while", "do",
45 "let", "in",
4648 "Z", "C", "A", "X", "Y"
4749 ],
4850 Token.reservedOpNames = [";", "~", "&", "="]
101103 s <- identifier
102104 return $ VarRef s
103105
106 routineP :: Parser Expr
107 routineP = do
108 reserved "routine"
109 e <- expr
110 return $ Routine e
111
112 callP :: Parser Expr
113 callP = do
114 reserved "call"
115 e <- expr
116 return $ Call e
117
104118 lda_i :: Parser Expr
105119 lda_i = reserved "LDA#" >> return LDA_i
106120 sta_a = reserved "STA" >> return STA_a
189203 return $ foldl Apply e es
190204
191205 expr2 :: Parser Expr
192 expr2 = ifP <|> repeatP <|> whileP <|> letP <|> parens expr <|> instruction <|> litInt <|> litLoc <|> varRef
206 expr2 = letP <|> ifP <|> repeatP <|> whileP
207 <|> callP <|> routineP
208 <|> litInt <|> litLoc
209 <|> parens expr
210 <|> instruction
211 <|> varRef
193212
194213 -- expression parser
195214 program :: Parser Expr