X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=src%2Fparser.mly;h=c3148cae191b50a466fa4cf09ab48d2e7037c45d;hb=976bb1b35d77f3058df3c25c1e2a4767147d606b;hp=69cb0637d53ca399c4af9a84f78425ff8c06423b;hpb=318cea9f1c7669d23d27fc362bf06b9aca1b61a1;p=goals.git diff --git a/src/parser.mly b/src/parser.mly index 69cb063..c3148ca 100644 --- a/src/parser.mly +++ b/src/parser.mly @@ -18,26 +18,66 @@ *) %{ +open Utils open Printf + +(* This is initialized with Lexer.read once the program + * starts. Doing this avoids a circular dependency caused + * by include files. + *) +let lexer_read = ref None + +let find_on_include_path filename = + if not (Filename.is_implicit filename) then filename + else ( + let rec loop = function + | [] -> filename + | inc :: incs -> + let path = inc // filename in + if Sys.file_exists path then path else loop incs + in + loop Cmdline.includes + ) + +let do_include env loc filename optflag file = + let filename = Eval.substitute env loc filename in + let filename = find_on_include_path filename in + if optflag && not (Sys.file_exists filename) then env + else ( + let fp = open_in filename in + let lexbuf = Lexing.from_channel fp in + lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = filename }; + let reader = + match !lexer_read with None -> assert false | Some r -> r in + let env' = file reader lexbuf in + close_in fp; + Ast.Env.merge env env' + ) %} (* Tokens. *) %token CODE %token COLON %token COMMA -%token ID %token EQUALS %token EOF +%token FUNCTION %token GOAL +%token ID +%token INCLUDE %token LEFT_ARRAY %token LEFT_PAREN %token LET +%token OPTINCLUDE %token RIGHT_ARRAY %token RIGHT_PAREN +%token SEMICOLON %token STRING +%token TACTIC +%token TACTIC_KEYWORD (* Start nonterminals. *) -%start file +%start file %start expr %% @@ -46,12 +86,15 @@ file: ; stmts: - | list(stmt) - { List.fold_left ( - fun env (name, expr) -> Ast.StringMap.add name expr env - ) Ast.StringMap.empty $1 - } + | (* none *) { Ast.Env.empty } + | stmts INCLUDE STRING option(SEMICOLON) + { do_include $1 $loc $3 false file } + | stmts OPTINCLUDE STRING option(SEMICOLON) + { do_include $1 $loc $3 true file } + | stmts stmt option(SEMICOLON) + { let name, expr = $2 in Ast.Env.add name expr $1 } ; + stmt: | option(goal_stmt) patterns COLON barelist option(CODE) { let name, params = @@ -59,31 +102,40 @@ stmt: | None -> sprintf "_goal@%d" $startpos.pos_lnum, [] | Some x -> x in - name, Ast.EGoal ($loc, (params, $2, $4, $5)) + name, Ast.EGoalDefn ($loc, (params, $2, $4, $5)) } | goal_stmt CODE { let name, params = $1 in - name, Ast.EGoal ($loc, (params, [], [], Some $2)) + name, Ast.EGoalDefn ($loc, (params, [], [], Some $2)) + } + | FUNCTION ID params_decl EQUALS CODE + { + $2, Ast.EFuncDefn ($loc, ($3, $5)) + } + | TACTIC_KEYWORD TACTIC params_decl EQUALS CODE + { + $2, Ast.ETacticDefn ($loc, ($3, $5)) } | LET ID EQUALS expr { $2, $4 } ; goal_stmt: - | GOAL ID option(param_decl) EQUALS + | GOAL ID option(params_decl) EQUALS { $2, match $3 with None -> [] | Some ps -> ps } ; -param_decl: - | LEFT_PAREN separated_list(COMMA, ID) RIGHT_PAREN { $2 } +params_decl: + | LEFT_PAREN separated_list(COMMA, param_decl) RIGHT_PAREN { $2 } ; +param_decl: + | ID { $1 } patterns: | separated_list(COMMA, pattern) { $1 } ; pattern: - | STRING { Ast.PTactic ($loc, "file", [$1]) } + | STRING { Ast.PTactic ($loc, "*file", [$1]) } | ID pattern_params { Ast.PTactic ($loc, $1, $2) } - | ID { Ast.PVar ($loc, $1) } ; pattern_params: | LEFT_PAREN separated_list(COMMA, pattern_param) RIGHT_PAREN { $2 } @@ -95,6 +147,7 @@ pattern_param: expr: | ID params { Ast.ECall ($loc, $1, $2) } | ID { Ast.EVar ($loc, $1) } + | TACTIC params { Ast.ETacticCtor ($loc, $1, $2) } | STRING { Ast.ESubsts ($loc, $1) } | LEFT_ARRAY barelist RIGHT_ARRAY { Ast.EList ($loc, $2) } ;