move expression's parsing to sw/lib/ocaml

This commit is contained in:
Pascal Brisset
2010-04-04 17:04:13 +00:00
parent 43fe75251e
commit 308f3fc160
8 changed files with 16 additions and 247 deletions
+4 -8
View File
@@ -32,20 +32,16 @@ OCAMLYACC=ocamlyacc
all: gen_aircraft.out gen_airframe.out gen_messages.out gen_ubx.out gen_flight_plan.out gen_radio.out gen_periodic.out gen_settings.out gen_tuning.out gen_xsens.out gen_modules.out gen_conf_radio_control_ppm.out find_free_msg_id.out
FP_CMO = fp_syntax.cmo fp_parser.cmo fp_lexer.cmo fp_proc.cmo gen_flight_plan.cmo
FP_CMO = fp_proc.cmo gen_flight_plan.cmo
ABS_FP = $(FP_CMO:%=$$PAPARAZZI_SRC/sw/tools/%)
gen_flight_plan.out : $(FP_CMO) ../lib/ocaml/lib-pprz.cma
@echo OL $@
$(Q)$(OCAMLC) $(INCLUDES) -custom -o $@ unix.cma str.cma xml-light.cma ivy-ocaml.cma lib-pprz.cma $^
fp_parser.cmo : fp_parser.cmi fp_syntax.cmi
fp_proc.cmo : fp_lexer.cmi fp_parser.cmi fp_proc.cmi
fp_parser.cmi : fp_parser.ml fp_syntax.cmi
fp_lexer.cmi : fp_syntax.cmi
fp_lexer.cmo : fp_lexer.cmi
gen_flight_plan.cmo : fp_parser.cmi fp_proc.cmi
fp_syntax.cmo : fp_syntax.cmi
fp_proc.cmo : fp_proc.cmi
gen_flight_plan.cmo : fp_proc.cmi
%.out : %.ml Makefile
-36
View File
@@ -1,36 +0,0 @@
(*
$Id$
*)
{
open Fp_parser
}
rule token = parse
[' ' '\t' '\n'] { token lexbuf}
| "/*"([^'*']|'*'[^'/'])*'*'*'/' { token lexbuf}
| ['0'-'9']+ { INT (int_of_string (Lexing.lexeme lexbuf)) }
| ['0'-'9']+'.'['0'-'9']* { FLOAT (float_of_string (Lexing.lexeme lexbuf)) }
| '$'?['a'-'z' '_' 'A'-'Z'] (['a'-'z' 'A'-'Z' '_' '.' '0'-'9']*) { IDENT (Lexing.lexeme lexbuf) }
| '\''[^'\'']+'\'' { let s = Lexing.lexeme lexbuf in IDENT (String.sub s 1 (String.length s - 2)) }
| ',' { COMMA }
| ';' { SEMICOLON }
| ':' { COLON }
| '(' { LP }
| ')' { RP }
| '{' { LC }
| '}' { RC }
| '[' { LB }
| ']' { RB }
| "==" { EQ }
| "&&" { AND }
| "||" { OR }
| ">" { GT }
| "%" { MOD }
| ">=" { GEQ }
| "+" { PLUS }
| "=" { ASSIGN }
| "-" { MINUS }
| "*" { MULT }
| "/" { DIV }
| "!" { NOT }
| eof { EOF }
-53
View File
@@ -1,53 +0,0 @@
/* $Id$ */
%{
open Fp_syntax
%}
%token <int> INT
%token <float> FLOAT
%token <string> IDENT
%token EOF
%token COMMA SEMICOLON LP RP LC RC LB RB AND COLON OR
%token EQ GT ASSIGN GEQ NOT
%token PLUS MINUS
%token MULT DIV MOD
%left AND OR /* lowest precedence */
%left EQ GT ASSIGN GEQ
%left PLUS MINUS
%left MULT DIV MOD
%nonassoc NOT
%nonassoc UMINUS /* highest precedence */
%start expression /* the entry point */
%type <Fp_syntax.expression> expression
%%
expression:
expression GT expression { CallOperator (">",[$1;$3]) }
| expression GEQ expression { CallOperator (">=",[$1;$3]) }
| expression EQ expression { CallOperator ("==",[$1;$3]) }
| expression AND expression { CallOperator ("&&",[$1;$3]) }
| expression OR expression { CallOperator ("||",[$1;$3]) }
| expression PLUS expression { CallOperator ("+",[$1;$3]) }
| expression MINUS expression { CallOperator ("-",[$1;$3]) }
| expression MULT expression { CallOperator ("*",[$1;$3]) }
| expression DIV expression { CallOperator ("/",[$1;$3]) }
| expression MOD expression { CallOperator ("%",[$1;$3]) }
| MINUS expression %prec UMINUS { CallOperator ("-",[$2]) }
| NOT expression { CallOperator ("!",[$2]) }
| INT { Int $1 }
| FLOAT { Float $1 }
| IDENT { Ident $1 }
| IDENT LP Args RP { Call ($1, $3) }
| LP expression RP { $2 }
| IDENT LB expression RB { Index ($1, $3) }
;
Args: { [] }
| expression NextArgs { $1::$2 }
;
NextArgs: { [] }
| COMMA expression NextArgs { $2::$3 }
;
+5 -5
View File
@@ -28,7 +28,7 @@ open Printf
module G2D = Geometry_2d
open Fp_syntax
open Expr_syntax
let rec list_split3 = function
[] -> ([], [], [])
@@ -38,7 +38,7 @@ let rec list_split3 = function
let parse_expression = fun s ->
let lexbuf = Lexing.from_string s in
try
Fp_parser.expression Fp_lexer.token lexbuf
Expr_parser.expression Expr_lexer.token lexbuf
with
Failure("lexing: empty token") ->
fprintf stderr "Lexing error in '%s': unexpected char: '%c' \n"
@@ -56,7 +56,7 @@ let subst_expression = fun env e ->
let rec sub = fun e ->
match e with
Ident i -> Ident (try List.assoc i env with Not_found -> i)
| Int _ | Float _ -> e
| Int _ | Float _ | Field _ -> e
| Call (i, es) -> Call (i, List.map sub es)
| CallOperator (i, es) -> CallOperator (i, List.map sub es)
| Index (i,e) -> Index (i,sub e) in
@@ -65,7 +65,7 @@ let subst_expression = fun env e ->
let transform_expression = fun env e ->
let e' = subst_expression env e in
Fp_syntax.sprint_expression e'
Expr_syntax.sprint e'
let transform_values = fun attribs_not_modified env attribs ->
@@ -204,7 +204,7 @@ let parse_include = fun dir flight_plan include_xml ->
and args_assocs = build_assocs "arg" "name" "value" include_xml in
try
let proc = ExtXml.parse_file f in
let proc = ExtXml.parse_file ~noprovedtd:true f in
let params = List.filter
(fun x -> ExtXml.tag_is x "param")
(Xml.children proc) in
+1 -1
View File
@@ -24,7 +24,7 @@
*
*)
val parse_expression : string -> Fp_syntax.expression
val parse_expression : string -> Expr_syntax.expression
val process_paths : Xml.xml -> Xml.xml
(** [process_paths flight_plan] *)
-90
View File
@@ -1,90 +0,0 @@
(*
$Id$
Syntax of flight plan expressions
*)
open Printf
type ident = string
type operator = string
type expression =
| Ident of ident
| Int of int
| Float of float
| Call of ident * (expression list)
| CallOperator of ident * (expression list)
| Index of ident * expression
let c_var_of_ident = fun x -> "_var_" ^ x
let rec sprint_expression = function
Ident i when i.[0] = '$' -> sprintf "%s" (c_var_of_ident (String.sub i 1 (String.length i - 1)))
| Ident i -> sprintf "%s" i
| Int i -> sprintf "%d" i
| Float i -> sprintf "%f" i
| CallOperator (op, [e1;e2]) ->
sprintf "(" ^ sprint_expression e1 ^ op ^ sprint_expression e2 ^ ")"
| CallOperator (op, [e1]) ->
sprintf "%s(%s)" op (sprint_expression e1)
| CallOperator (_,_) -> failwith "Operator should be binary or unary"
| Call (i, es) ->
let ses = List.map sprint_expression es in
sprintf "%s(" i ^ String.concat "," ses ^ ")"
| Index (i,e) -> sprintf "%s[" i ^ sprint_expression e ^ "]"
(* Valid functions : FIXME *)
let functions = [
"Qdr";
"And";
"Or";
"RcRoll";
"RcEvent1";
"RcEvent2";
"RadOfDeg"]
(* Valid identifiers : FIXME *)
let variables = [
"launch";
"estimator_z";
"estimator_flight_time";
"estimator_hspeed_mod";
"estimator_theta";
"circle_count";
"vsupply";
"stage_time";
"stage_time_ds";
"block_time";
"SECURITY_ALT";
"ground_alt"; "GROUND_ALT";
"TRUE";
"FALSE";
"QFU";
"gps_mode"; "gps_utm_east"; "gps_utm_north"; "gps_utm_zone";
"nav_utm_east0"; "nav_utm_north0"; "nav_utm_zone0"; "cruise_throttle"; "gps_lost"
]
exception Unknown_ident of string
exception Unknown_operator of string
exception Unknown_function of string
let unexpected = fun kind x ->
fprintf stderr "Warning: unexpected %s in expression: '%s' \n" kind x
let rec check_expression = fun e ->
match e with
Ident i when i.[0] = '$' -> ()
| Ident i ->
if not (List.mem i variables) then
unexpected "ident" i
| Int _ | Float _ | CallOperator _ -> ()
| Call (i, es) ->
if not (List.mem i functions) then
unexpected "function" i;
List.iter check_expression es
| Index (i,e) ->
if not (List.mem i variables) then
unexpected "ident" i;
check_expression e
-48
View File
@@ -1,48 +0,0 @@
(*
* $Id$
*
* Syntax of flight plan parsed expressions
*
* Copyright (C) 2004 CENA/ENAC, Pascal Brisset, Antoine Drouin
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*)
type ident = string
type operator = string
type expression =
| Ident of ident
| Int of int
| Float of float
| Call of ident * expression list
| CallOperator of ident * expression list
| Index of ident * expression
val c_var_of_ident : ident -> string
(** Encapsulate a user ident into a C variable *)
val sprint_expression : expression -> string
exception Unknown_ident of string
exception Unknown_operator of string
exception Unknown_function of string
val check_expression : expression -> unit
(** May raise [Unknown_ident], [Unknown_operator] or [Unknown_function]
exceptions *)
+6 -6
View File
@@ -50,14 +50,14 @@ let parse = fun s ->
exit 1 in
begin
try
Fp_syntax.check_expression e
Expr_syntax.check_expression e
with
Fp_syntax.Unknown_operator x -> unexpected "operator" x
| Fp_syntax.Unknown_ident x -> unexpected "ident" x
| Fp_syntax.Unknown_function x -> unexpected "function" x
Expr_syntax.Unknown_operator x -> unexpected "operator" x
| Expr_syntax.Unknown_ident x -> unexpected "ident" x
| Expr_syntax.Unknown_function x -> unexpected "function" x
end
end;
Fp_syntax.sprint_expression e
Expr_syntax.sprint e
let parsed_attrib = fun xml a ->
parse (ExtXml.attrib xml a)
@@ -304,7 +304,7 @@ let rec print_stage = fun index_of_waypoints x ->
| "for" ->
let f = gen_label "for" in
let e = gen_label "endfor" in
let v = Fp_syntax.c_var_of_ident (ExtXml.attrib x "var")
let v = Expr_syntax.c_var_of_ident (ExtXml.attrib x "var")
and from_ = parsed_attrib x "from"
and to_expr = parsed_attrib x "to" in
let to_var = v ^ "_to" in