我刚刚写完这段模式匹配(下面附上)。无论我如何尝试修复它,当我尝试编译它时仍然存在“错误:语法错误”。
这段代码在这里:
let rec stmt (s:Ast.stmt) : X86.inst list =
let get_label e =
let inst = stmt e in
let pattern = (List.nth (List.rev inst) 0) in
let label = match pattern with
|Memory (Store (Addr (Label l), Reg r)) -> l
|_ -> "Error"
in
match s with
|Exp e -> (match e with
|Int i -> [movei RAX i; store new_temp() RAX]
|Var v -> [load RBX v; store new_temp() RBX]
|Binop (e1, op, e2) -> let (label1, label2) = (get_label e1, get_label e2)
in let operation =
match op with
|Plus -> [Arith (Add (RAX, Reg RBX))]
|Minus -> [Arith (Sub (RAX, Reg RBX))]
|Times -> [Arith (Mul RBX)]
|Div -> [Arith (Div RBX)]
|Eq -> [Arith (Cmp (RAX, RBX))]
|Neq -> [Arith (Cmp (RAX, Reg RBX)); set NE RAX]
|Lt -> [Arith (Cmp (RAX, Reg RBX)); set L RAX]
|Lte -> [Arith (Cmp (RAX, Reg RBX)); set LE RAX]
|Gt -> [Arith (Cmp (RAX, Reg RBX)); set G RAX]
|Gte -> [Arith (Cmp (RAX, Reg RBX)); set GE RAX]
in [load RAX label1; load RBX label2] @ operation @ [store (new_temp()) RAX]
|Not e -> let label = get_label e in
[load RBX label; Arith (Neg RBX); store (new_temp()) RAX]
|And (e1, e2) -> let (label1, label2) = (get_label e1, get_label e2) in
[load RAX label1; load RBX label2; Bitop (And (RAX, Reg RBX)); store (new_temp()) RAX]
|Or (e1, e2) -> let (label1, label2) = (get_label e1, get_label e2) in
[load RAX label1; load RBX label2; Bitop (Or (RAX, Reg RBX)); store (new_temp()) RAX]
|Assign (e1, e2) -> let label = get_label e2 in
[movei RAX label; store e1 RAX; store (new_temp()) RAX]
|_ -> [])
|Block e -> []
|If (l, m, r) -> []
|While (l, r) -> []
|For (l, m1, m2, r) -> []
|Return e -> []
|_ -> []
let compile (p : Ast.program) : result =
let _ = reset() in
let _ = collect_vars(p) in
let insts = List.concat_map stmt p in
{ code = insts; data = VarSet.elements !variables }
错误在let compile (p: Ast.program) : result =
这是(来自外壳):
110 | let compile (p : Ast.program) : result =
^^^
Error: Syntax error