简体   繁体   中英

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 =

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:

        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.

I suspect what you want is to just skip the label binding:

        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. That usually helps to avoid mistakes like this:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM