Ocamlyacc seemingly not returning a full record

80 Views Asked by At

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.

1

There are 1 best solutions below

1
On BEST ANSWER

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)= ….