[英]Why this syntax error? No given reason from the shell
I've just finished writing this piece of pattern matching (to be attached below).我刚刚写完这段模式匹配(下面附上)。 No matter how I tried fixing it up, there is still "Error: Syntax error" when I tried to compile it.
无论我如何尝试修复它,当我尝试编译它时仍然存在“错误:语法错误”。
The piece of code is here:这段代码在这里:
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 }
Error is at let compile (p: Ast.program): result =
错误在于
let compile (p: Ast.program): result =
which is this (from the shell):这是(来自外壳):
110 | let compile (p : Ast.program) : result =
^^^
Error: Syntax error
The error follows from not having properly completed the get_label
function:错误源于未正确完成
get_label
function:
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"
Hence the in
following it is associated with let label =...
, not let get_label e =...
, despite your indentation.因此
in
尽管您有缩进,但以下它与let label =...
,而不是let get_label e =...
相关联。
I suspect what you want is to just skip the label
binding:我怀疑你想要的是跳过
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"
It might be a good idea to use a tool like ocp-indent
, which will indent your code according to how the compiler will interpret the code, not how you want the compiler to interpret it.使用像
ocp-indent
这样的工具可能是个好主意,它会根据编译器解释代码的方式来缩进你的代码,而不是你希望编译器如何解释它。 That usually helps to avoid mistakes like this:)这通常有助于避免这样的错误:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.