简体   繁体   中英

Ocamlyacc seemingly not returning a full record

I have the following parser which should return a record with globalVars and globalFns , but it doesn't appear to.

%start program
%type <Ast.program> program

%%

program:
    decls EOF { $1 }

decls:
      /* nothing */             { { globalVars = []; globalFns = []; } }
    | decls varDecl             { { $1 with globalVars  = $1::$2.globalVars  } }
    | decls fnDecl              { { $1 with globalFns   = $1::$2.globalFns  } }

where ast.ml defines a program as:

type program = {
    globalVars  : bind list;
    globalFns   : func_decl list;
}

The error I'm receiving is: Error: Unbound record field globalVars when I try to do something like:

let translate program =
    let global_vars =
        let global_var m (t, n) =
            let init = L.const_int (ltype_of_typ t) 0
            in StringMap.add n (L.define_global n init the_module) m in
            List.fold_left global_var StringMap.empty program.globalVars in

I simply cannot figure out why program.globalVars is unbound here. If someone could point me in the proper direction, that'd be very much appreciated.

It is the record field globalVars that is unbound in the current scope not really program.globalVars itself: field labels lives in the module that defines them. In order to access a record field for a type defined outside of the current module; one needs to either use a fully qualified field paths, program.Ast.globalVars where Ast.globalVars is the path to the field globalVars defined in the module Ast or bring the field in scope, by opening the module for instance or annotating the type of program: let translate (program:Ast.program)= … .

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