add interpreter code and tests for loop counter instructions
[fleet.git] / src / edu / berkeley / fleet / assembler / fleet.g
1 // The FLEET Assembly Language Grammar [26-Aug-2007]
2
3 // Note that this is the *entire, complete* formal specification of
4 // the grammar.  An equivalent lex+yacc grammar and support code would
5 // be several times as long.
6
7 s               = ws Program ws
8 Program::       = Directives CodeBagBody /ws
9 Directives::    = Directive */ ws
10 CodeBagBody::   = (Fiber | CodeBagDef) */ ws
11 CodeBagDef::    = CodeBagName ":" "{" CodeBagBody "}" /ws
12 Fiber::         = Pump        ":" Instructions        /ws
13
14 Instructions::  = Instruction +/ ws
15 Instruction     = ^"unclog"                          ";" /ws
16                 | ^"clog"                            ";" /ws
17                 | ^"massacre"                        ";" /ws
18                 | ^"kill"                            ";" /ws
19                 |  "take" ^"loop" "counter"                            ";" /ws
20                 | ^"load" (^"loop"|^"repeat") ws "counter"             ";" /ws
21                 |  "load" (^"loop"|^"repeat") ws "counter" ^"with" int ";" /ws
22                 | ^"decrement" "loop" "counter"                        ";" /ws
23                 | ^"setflags" ws "a=" Flag ws "b=" Flag ws ";"
24                 | ^"kill"     int                    ";" /ws
25                 | ^"literal"  Literal  RequeueCount  ";" /ws
26                 | RepeatCount Commands RequeueCount ^";" /ws
27 RepeatCount     = "" | ^"[*]" | "[" int "]"
28 RequeueCount    = ""
29                 | "," "requeue" (^"forever" | int ws "times") /ws
30
31 Flag =  ^"0"
32      |  ^"1"
33      |  ^"a"
34      |  ^"b"
35      |  ^"s"
36      |  ^"z"
37      | ^"!a"
38      | ^"!b"
39      | ^"!s"
40      | ^"!z"
41
42 Commands::      = Command +/ (ws "," ws)
43 Command         = ^"wait"
44                 | ^"nop"
45                 | ^"discard"
46                 | ^"recieve"
47                 | ^"take"
48                 | ^"deliver"
49                 | ^"send"
50                 | ^"sendto"     Destination /ws
51                 | ^"notify"     Destination /ws
52                 | ^"notifyLast" Destination /ws
53
54 Literal         = int
55                 | ShipType "." PortName ^"[" Constants "]"
56                 | CodeBagName
57                 | "{" CodeBagBody "}" /ws
58
59 Pump::          = ShipName "." PortName
60 Destination::   = ShipName "." PortName
61 Constants::     = Constant +/ ","
62 Constant::      = [a-zA-Z0-9=_\-]++
63
64 CodeBagName     = UpcaseWord
65 ShipType        = UpcaseWord
66 ShipName        = DowncaseWord
67 PortName        = DowncaseWord
68 Word            = UpcaseWord | DowncaseWord
69 UpcaseWord      = Name:: [A-Z]    ("":: [A-Za-z0-9_]**)
70 DowncaseWord    = Name:: [a-z]    ("":: [A-Za-z0-9_]**)
71 int::           = (^"-"|^"") (^"0x"|^"0b"|^"") (digits:: [0-9]++)
72
73 ws!             = wsx -> ~[\r\n ]
74 wsx!            = ()
75                 | wsx [\r\n ]
76                 | wsx Comment
77 Comment!        = "//" ~[\n\r]* [\r\n]
78                 | "/*" ~[\n\r]* "*/"
79
80 // the following extensions are not part of the official FLEET syntax
81
82 Directive      = ^"#memory" "{" (Memory:: int +/ (ws! "," ws!)) "}" /ws
83                | ^"#import" [A-Za-z_.]++ /ws
84                | ^"#include" ws! "\"" ~[\"]+ "\""
85                | ^"#ship" ShipName ":" ("":: [0-9A-Za-z_.]++) /ws
86                | ^"#expect" int /ws
87                | ^"#skip"