-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbsyn.fs
68 lines (59 loc) · 3.38 KB
/
Absyn.fs
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
(* File MicroC/Absyn.fs
Abstract syntax of micro-C, an imperative language.
[email protected] 2009-09-25
Must precede Interp.fs, Comp.fs and Contcomp.fs in Solution Explorer
*)
module Absyn
// 基本类型
// 注意,数组、指针是递归类型
// 这里没有函数类型,注意与上次课的 MicroML 对比
type typ =
| TypI (* Type int *)
| TypC (* Type char *)
| TypA of typ * int option (* Array type *)
| TypP of typ (* Pointer type *)
| TypB (* Type bool *)
and expr = // 表达式,右值
| Access of access (* x or *p or a[e] *) //访问左值(右值)
| Assign of access * expr (* x=e or *p=e or a[e]=e *)
| Addr of access (* &x or &*p or &a[e] *)
| CstI of int (* Constant *)
| Prim1 of string * expr (* Unary primitive operator *)
| Prim2 of string * expr * expr (* Binary primitive operator *)
| Prim3 of expr * expr * expr
| Andalso of expr * expr (* Sequential and *)
| Orelse of expr * expr (* Sequential or *)
| Call of string * expr list (* Function call f(...) *)
| HexChange of string * int (* Hexchange *)
| Inc of access
| Sub of access
| Self of access * string * expr
and access = //左值,存储的位置
| AccVar of string (* Variable access x *)
| AccDeref of expr (* Pointer dereferencing *p *)
| AccIndex of access * expr (* Array indexing a[e] *)
and stmt =
| If of expr * stmt * stmt (* Conditional *)
| While of expr * stmt (* While loop *)
| DoWhile of stmt * expr (* DoWhile loop *)
| DoUntil of stmt * expr (* DoUntil loop *)
| Expr of expr (* Expression statement e; *)
| Return of expr option (* Return from method *)
| Block of stmtordec list (* Block: grouping and scope *)
| For of expr * expr * expr * stmt (* For loop *)
| Switch of expr * stmt list
| Case of expr * stmt
| Default of stmt
| Break
| Continue
// 语句块内部,可以是变量声明 或语句的列表
and stmtordec =
| Dec of typ * string (* Local variable declaration *)
| Stmt of stmt (* A statement *)
// 顶级声明 可以是函数声明或变量声明
and topdec =
| Fundec of typ option * string * (typ * string) list * stmt
| Vardec of typ * string
// 程序是顶级声明的列表
and program =
| Prog of topdec list