为什么出现这种语法错误?外壳中没有给出任何原因



我刚刚完成了这段模式匹配的编写(将附在下面(。无论我如何修复它,仍然存在";错误:语法错误";当我试图编译它时。

代码在这里:

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

未正确完成get_label功能会导致错误:

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"

因此,它后面的inlet label = ...关联,而不是与let get_label e = ...关联,尽管有缩进。

我怀疑你想要的只是跳过label绑定:

let get_label e = 
let inst = stmt e in 
let pattern = (List.nth (List.rev inst) 0) in
match pattern with 
| Memory (Store (Addr (Label l), Reg r)) -> l 
| _ -> "Error"

使用像ocp-indent这样的工具可能是个好主意,它会根据编译器解释代码的方式而不是你希望编译器解释它的方式来缩进代码。这通常有助于避免这样的错误:(

相关内容

  • 没有找到相关文章

最新更新