Skip to content

Commit

Permalink
Fixed small things, script callable from prompt using modeq scriptnam…
Browse files Browse the repository at this point in the history
…e.mos.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1090 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Feb 3, 2004
1 parent ef8ef0d commit d18d417
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 45 deletions.
53 changes: 53 additions & 0 deletions modeq/absyn_builder/parse.cpp
Expand Up @@ -247,5 +247,58 @@ RML_BEGIN_LABEL(Parser__parsestringexp)
}
RML_END_LABEL


RML_BEGIN_LABEL(Parser__parseexp)
{
char * filename = RML_STRINGDATA(rmlA0);
bool debug = check_debug_flag("parsedump");
try
{
std::ifstream stream(filename);

modelica_lexer lex(stream);
modelica_expression_parser parse(lex);
ANTLR_USE_NAMESPACE(antlr)ASTFactory factory;
parse.initializeASTFactory(factory);
parse.setASTFactory(&factory);
parse.interactiveStmts();
antlr::RefAST t = parse.getAST();

if (t)
{
if (debug)
{
//std::cout << "parsedump not implemented for interactiveStmt yet"<<endl;
//parse_tree_dumper dumper(std::cout);
//dumper.dump(t);
}

modelica_tree_parser build;
build.initializeASTFactory(factory);
build.setASTFactory(&factory);
void* ast = build.interactive_stmt(t);

if (debug)
{
std::cout << "Build done\n";
}

rmlA0 = ast ? ast : mk_nil();

RML_TAILCALLK(rmlSC);
}
}
catch (std::exception &e)
{
std::cerr << "Error while parsing expression:\n" << e.what() << "\n";
}
catch (...)
{
//std::cerr << "Error while parsing expression\n";
}
RML_TAILCALLK(rmlFC);
}
RML_END_LABEL

} // extern "C"

37 changes: 32 additions & 5 deletions modeq/ceval.rml
Expand Up @@ -153,6 +153,7 @@ relation ceval : (Env.Env, Exp.Exp, bool, Interactive.InteractiveSymbolTable opt
=> (newval,st)

rule (* Call externally implemented functions. *)
Static.is_function_in_cflist(cflist,func) => true &
ceval_list(env,expl,true,st) => vallst &
ModUtil.path_string2(func,"_") => funcstr &
string_append(funcstr,"_in.txt") => infilename &
Expand All @@ -161,7 +162,7 @@ relation ceval : (Env.Env, Exp.Exp, bool, Interactive.InteractiveSymbolTable opt
System.execute_function(funcstr) &
System.read_values_from_file(outfilename) => newval
-------------------------------------------------------
ceval (env, e as Exp.CALL(func,expl,_,_),true,st as SOME(_)) => (newval,st)
ceval (env, e as Exp.CALL(func,expl,_,_),true,st as SOME(Interactive.SYMBOLTABLE(p,_,_,_,cflist))) => (newval,st)

(** Strings **)

Expand Down Expand Up @@ -270,6 +271,21 @@ relation ceval : (Env.Env, Exp.Exp, bool, Interactive.InteractiveSymbolTable opt
ceval (env, Exp.BINARY(lh, Exp.MUL(Exp.REAL), rh),impl,st)
=> (Values.REAL(sum),st'')

rule ceval (env,lh,impl,st) => (Values.REAL(lhv),st') &
ceval (env,rh,impl,st') => (Values.REAL(rhv),st'') &
real_div(lhv, rhv) => div
-------------------------
ceval (env, Exp.BINARY(lh, Exp.DIV(Exp.REAL), rh),impl,st)
=> (Values.REAL(div),st'')

rule ceval (env,lh,impl,st) => (Values.REAL(lhv),st') &
ceval (env,rh,impl,st') => (Values.REAL(rhv),st'') &
not real_div(lhv,rhv) => _ &
Print.print_buf "#Error, division by zero.\n"
-------------------------
ceval (env, Exp.BINARY(lh, Exp.DIV(Exp.REAL), rh),impl,st)
=> fail

rule ceval (env,lh,impl,st) => (Values.INTEGER(lhv),st') &
ceval (env,rh,impl,st') => (Values.INTEGER(rhv),st'') &
int_mul(lhv, rhv) => sum
Expand Down Expand Up @@ -421,9 +437,18 @@ end
relation ceval_function: (Env.Env, Absyn.Path, Values.Value list)
=> Values.Value =

rule
rule (* For record constructors *)
Lookup.lookup_record_constructor_class(env,funcname) => (_,_) &
Lookup.lookup_class(env,funcname,false) => (c,env') &
SCode.component_names(c) => compnames &
Types.values_to_mods(vallst,compnames) => mod &
Inst.inst_class(env',mod,Prefix.NOPRE,[],c,[],false,Inst.TOP_CALL)
=> (dae,_,_,_,_) &
DAE.dump_elements dae &
Print.get_string => s & print s & print "\n" &
DAE.dae_to_record_value(dae) => value
----------------------
ceval_function(env,funcname,vallst) => fail
ceval_function(env,funcname,vallst) => value

end

Expand Down Expand Up @@ -875,9 +900,11 @@ relation ceval_interactive_functions: (Env.Env, Exp.Exp, Interactive.Interactive

rule System.read_file(str) => scriptstr &
Parser.parsestringexp(scriptstr) => istmts &
Interactive.evaluate(istmts,st) => (res,newst)
Interactive.evaluate(istmts,st,true) => (res,newst) &
Util.string_append_list([res,"\ntrue"]) => res'
--------------------------
ceval_interactive_functions (env, Exp.CALL(Absyn.IDENT("runScript"),[Exp.SCONST(str)],_,_),st as Interactive.SYMBOLTABLE(p,sp,ic,iv,cf)) => (Values.BOOL(true),newst)
ceval_interactive_functions (env, Exp.CALL(Absyn.IDENT("runScript"),[Exp.SCONST(str)],_,_),st as Interactive.SYMBOLTABLE(p,sp,ic,iv,cf))
=> (Values.STRING(res'),newst)

axiom ceval_interactive_functions (env, Exp.CALL(Absyn.IDENT("runScript"),[Exp.SCONST(str)],_,_),st)
=> (Values.BOOL(false),st)
Expand Down
24 changes: 24 additions & 0 deletions modeq/dae.rml
Expand Up @@ -38,6 +38,7 @@ module DAE:
with "exp.rml"
with "algorithm.rml"
with "types.rml"
with "values.rml"

type Ident = string
type InstDims = Exp.Subscript list
Expand Down Expand Up @@ -125,6 +126,7 @@ module DAE:
relation get_variable_bindings_str: Element list => string
relation to_flow: bool => Flow
relation get_flow_variables: Element list => Exp.ComponentRef list
relation dae_to_record_value: Element list => Values.Value
end

(** - Relations
Expand All @@ -140,6 +142,7 @@ with "graphviz.rml"
with "dump.rml"
with "print.rml"
with "util.rml"
with "ceval.rml"

(** relation: dump
**
Expand Down Expand Up @@ -1553,4 +1556,25 @@ relation get_flow_variables_2: (Exp.ComponentRef list ,Ident)
Exp.join_crefs(Exp.CREF_IDENT(id,[]),cr) => cr'
----------------------------
get_flow_variables_2(cr::xs,id) => cr'::res
end

relation dae_to_record_value: (Element list) => Values.Value =
axiom dae_to_record_value([]) => Values.RECORD([],[])

(* rule Ceval.ceval([],e,true,NONE) => (value,_) &
dae_to_record_value(rest) => Values.RECORD(vals,names)
--------------------------------------------------
dae_to_record_value(VAR(Exp.CREF_IDENT(id,_),_,_,_,SOME(e),_,_,_)::rest)
=> Values.RECORD(value::vals,id::names)*)

rule Ceval.ceval([],rhs,false,NONE) => (value,_) &
dae_to_record_value(rest) => Values.RECORD(vals,names)
--------------------------------------------------
dae_to_record_value(EQUATION(Exp.CREF(Exp.CREF_IDENT(id,_),_),rhs)::rest)
=> Values.RECORD(value::vals,id::names)

rule dae_to_record_value(rest) => res
--------------------------------------------------
dae_to_record_value(_::rest) => res

end
17 changes: 17 additions & 0 deletions modeq/explode.rml
Expand Up @@ -158,6 +158,7 @@ module SCode :
relation print_restr: Restriction => ()
relation variability_string: Variability => string
relation count_parts: Class => int
relation component_names: Class => string list
end


Expand Down Expand Up @@ -840,4 +841,20 @@ relation count_parts: Class => int =

axiom count_parts _ => 0

end

relation component_names: Class => string list =

rule component_names_from_elts(elts) => res
--------------------------
component_names(CLASS(_,_,_,_,PARTS(elts,_,_,_,_,_))) => res
axiom component_names(_) => []
end
relation component_names_from_elts:(Element list) => string list =

axiom component_names_from_elts([]) => []

rule component_names_from_elts(rest) => res
--------------------------------------
component_names_from_elts(COMPONENT(id,_,_,_,_,_,_)::rest) => id::res
end
27 changes: 1 addition & 26 deletions modeq/inst.rml
Expand Up @@ -447,34 +447,9 @@ relation inst_class_in: (Env, Mod, Prefix, Connect.Sets, ClassInf.State,
=> (DAE.Element list, Env, Connect.Sets, ClassInf.State,
Types.Var list) =

axiom inst_class_in(env,mods,pre,csets,ci_state,
c as SCode.CLASS("Real",_,_,_,_),_,_, impl,packimpl)
=> ([] (* No DAE *), env, [], ci_state, [])

axiom inst_class_in(env,mods,pre,csets,ci_state,
c as SCode.CLASS("Integer",_,_,_,_),_,_, impl,packimpl)
=> ([] (* No DAE *), env, [], ci_state, [])

axiom inst_class_in(env,mods,pre,csets,ci_state,
c as SCode.CLASS("String",_,_,_,_),_,_, impl,packimpl)
=> ([] (* No DAE *), env, [], ci_state, [])

axiom inst_class_in(env,mods,pre,csets,ci_state,
c as SCode.CLASS("Boolean",_,_,_,_),_,_, impl,packimpl)
=> ([] (* No DAE *), env, [], ci_state, [])


rule (*clock() => t1 &*)
inst_classdef(env,mods,pre,csets,ci_state,d,r,prot,inst_dims
rule inst_classdef(env,mods,pre,csets,ci_state,d,r,prot,inst_dims
,impl,packimpl)
=> (l,env', csets', ci_state', tys)
(* & clock() => t2 &
real_sub(t2,t1) => time &
real_gt(time,1.5) => b &
real_string time =>timestr &
Util.string_append_list(["inst_class_in ", n, ": ", timestr, "\n" ]) => s1 &
Dump.select_string(b,s1,"") => str &
print str &*)
(*Prefix.print_prefix_str pre => prestr &
string_append(prestr, " : ") => prestr' &
string_append(prestr', n) => prestr'' *)
Expand Down
40 changes: 31 additions & 9 deletions modeq/interactive.rml
Expand Up @@ -49,7 +49,8 @@ module Interactive:
InteractiveVariable list * (*List of variables with values*)
(Absyn.Path * Types.Type) list (* List of compiled functions, F.Q name + type*)

relation evaluate: (InteractiveStmts, InteractiveSymbolTable) => (string,InteractiveSymbolTable)
relation evaluate: (InteractiveStmts, InteractiveSymbolTable, bool)
=> (string,InteractiveSymbolTable)

relation update_program: (Absyn.Program,Absyn.Program) => Absyn.Program
relation update_scope: (Absyn.Program, InteractiveVariable list) => InteractiveVariable list
Expand Down Expand Up @@ -103,20 +104,32 @@ val Graphics_records = "type LinePattern=enumeration(None,Solid,Dash,Dot,DashDot
** This relation evaluates expressions feeded interactively to the compiler.
**)

relation evaluate: (InteractiveStmts, InteractiveSymbolTable) => (string,InteractiveSymbolTable) =
relation evaluate: (InteractiveStmts, InteractiveSymbolTable, bool)
=> (string,InteractiveSymbolTable) =

rule evaluate2(ISTMTS([x],true),st) => (res,newst)
----------------------------
evaluate (ISTMTS([x],true),st) => ("",newst)
evaluate (ISTMTS([x],true),st,false) => ("",newst)

rule (* Verbose, semicolon off *)
evaluate2(ISTMTS([x],false),st) => (res,newst)
----------------------------
evaluate (ISTMTS([x],true),st,true) => (res,newst)

rule evaluate2(ISTMTS([x],false),st) => (res,newst)
----------------------------
evaluate (ISTMTS([x],false),st) => (res,newst)
evaluate (ISTMTS([x],false),st,verbose) => (res,newst)

rule evaluate2(ISTMTS([x],sc),st) => (res,newst) &
evaluate(ISTMTS(xs,sc),newst,false) => (res,newst')
------------------------------------------
evaluate (ISTMTS(x::xs,sc),st,false) => (res,newst')

rule evaluate2(ISTMTS([x],sc),st) => (res,newst) &
evaluate(ISTMTS(xs,sc),newst) => (res,newst')
evaluate(ISTMTS(xs,sc),newst,true) => (res2,newst') &
Util.string_append_list([res,res2]) => res'
------------------------------------------
evaluate (ISTMTS(x::xs,sc),st) => (res,newst')
evaluate (ISTMTS(x::xs,sc),st,true) => (res',newst')
end

relation evaluate2: (InteractiveStmts, InteractiveSymbolTable) => (string,InteractiveSymbolTable) =
Expand Down Expand Up @@ -149,15 +162,24 @@ relation evaluate_expr: (InteractiveStmt, InteractiveSymbolTable) =>
(string,InteractiveSymbolTable) =

rule build_env_from_symboltable(st) => env &
print "built env\n" &
Static.elab_exp(env,exp,true,SOME(st)) => (sexp,prop,SOME(st')) &
print "elabed expr\n" &
Ceval.ceval (env,sexp,true,SOME(st')) => (value,SOME(st'')) &
print "evaluated expr\n" &
Values.val_string value => str
-------------------------------
evaluate_expr(IEXP(exp),st as SYMBOLTABLE(p,_,_,_,_)) => (str,st'')

rule build_env_from_symboltable(st) => env &
not Static.elab_exp(env,exp,true,SOME(st)) => (sexp,prop,SOME(st')) &
Print.print_buf "# Error determing type of expression."
-------------------------------
evaluate_expr(IEXP(exp),st as SYMBOLTABLE(p,_,_,_,_)) => fail

rule build_env_from_symboltable(st) => env &
Static.elab_exp(env,exp,true,SOME(st)) => (sexp,prop,SOME(st')) &
not Ceval.ceval (env,sexp,true,SOME(st')) => (_,_) &
Print.print_buf "# Constant evaluating of expression failed.\n"
-------------------------------
evaluate_expr(IEXP(exp),st as SYMBOLTABLE(p,_,_,_,_)) => fail

rule build_env_from_symboltable(st) => env &
Static.elab_exp(env,cond,true,SOME(st))
Expand Down
5 changes: 4 additions & 1 deletion modeq/lookup.rml
Expand Up @@ -185,6 +185,10 @@ relation lookup_class: (Env.Env, Absyn.Path, bool) => (SCode.Class, Env.Env) =
----------------------------------------------------------
lookup_class(env, path, _) => (c, env')

rule lookup_class_in_env(env,path,false) => (c, env')
--------------------------------------
lookup_class(env, path as Absyn.IDENT(name),msgflag) => (c, env')


rule (* If we search for A1.A2....An.x while in scope A1.A2...An
, just search for x. Must do like this to ensure finite recursion *)
Expand Down Expand Up @@ -758,7 +762,6 @@ relation lookup_functions_in_frame: (Env.HashTable, (*Classes and vars*)
lookup_functions_in_frame(ht,httypes,env,id) => fail

rule Env.hash_get(ht, id, Env.myhash) => Env.CLASS(cdef as SCode.CLASS(n,_,_,SCode.R_RECORD,_),cenv) &
Print.print_buf "Found record when looking for function. Assuming implicit record constructor\n" &
build_record_constructor_varlst (cdef,env) => varlst &
Inst.package_prefix(cenv,Absyn.IDENT(n)) => fpath &
Types.make_function_type(fpath,varlst) => ftype
Expand Down

0 comments on commit d18d417

Please sign in to comment.