This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAbsyn.fs
66 lines (57 loc) · 3.2 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
(* File MicroC/Absyn.fs
Abstract syntax of micro-C, an imperative language.
sestoft@itu.dk 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 *)
| TypF (* Type float *)
| TypA of typ * int option (* Array type *)
| TypP of typ (* Pointer type *)
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 *)
| CstF of float32
| CstC of char
| 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(...) *)
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
| Expr of expr (* Expression statement e; *)
| For of expr * expr * expr * stmt
| Return of expr option (* Return from method *)
| Block of stmtordec list (* Block: grouping and scope *)
| Break
| Continue
| Switch of expr * stmt list
| Case of expr * stmt
// 语句块内部,可以是变量声明 或语句的列表
and stmtordec =
| Dec of typ * string (* Local variable declaration *)
| DecAndAssign of typ * string * expr
| Stmt of stmt (* A statement *)
// 顶级声明 可以是函数声明或变量声明
and topdec =
| Fundec of typ option * string * (typ * string) list * stmt
| Vardec of typ * string
| VardecAndAssign of typ * string * expr
// 程序是顶级声明的列表
and program =
| Prog of topdec list