From 93f29e63c106bf9b80008932e005625023a535a4 Mon Sep 17 00:00:00 2001 From: Peter Aronsson Date: Tue, 4 Feb 2003 08:10:09 +0000 Subject: [PATCH] Added module classloader and function loadClass that loads a class from MODELICAPATH. git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@913 f25d12d1-65f4-0310-ae8a-bbce733d8d8e --- modeq/Makefile.in | 59 ++++++---- modeq/absyn_builder/walker.g | 16 ++- modeq/classloader.rml | 217 +++++++++++++++++++++++++++++++++++ modeq/dump.rml | 3 +- modeq/inst.rml | 2 + modeq/interactive.rml | 73 ++++++++---- modeq/runtime/systemimpl.c | 94 +++++++++++++++ modeq/system.rml | 6 + 8 files changed, 421 insertions(+), 49 deletions(-) create mode 100644 modeq/classloader.rml diff --git a/modeq/Makefile.in b/modeq/Makefile.in index 8dd20ab275a..db18c5bdd2f 100644 --- a/modeq/Makefile.in +++ b/modeq/Makefile.in @@ -6,7 +6,7 @@ # $Id$ # - +ARCH = $(shell uname) ANTLR_HOME = @antlrhome@ ANTLR_INCP = -I@antlrinc@ @@ -19,7 +19,18 @@ RMLHOME = @rmlhome@ RML = @rmlc_bin@ -g RMLINC = -I$(RMLHOME)/include/plain +LDFLAGS= +ifeq ($(ARCH),CYGWIN_NT-5.1) +LDFLAGS = -L$(RMLHOME)/lib/plain $(ANTLR_LIBP) -lrml -lm -lantlr +endif + +ifeq ($(ARCH),CYGWIN_NT-5.0) +LDFLAGS = -L$(RMLHOME)/lib/plain $(ANTLR_LIBP) -lrml -lm -lantlr +endif + +ifeq ($(strip $(LDFLAGS)),) LDFLAGS = -L$(RMLHOME)/lib/plain $(ANTLR_LIBP) -lrml -lm -lantlr -lsocket -lnsl +endif PROG = modeq AST = absyn_builder/absyn_builder.a @@ -29,6 +40,7 @@ SRCRML= absyn.rml \ algorithm.rml \ builtin.rml \ classinf.rml \ + classloader.rml \ codegen.rml \ connect.rml \ dae.rml \ @@ -107,28 +119,29 @@ depend: $(RM) .dep Makefile.in.save ## dependencies -absyn.o: absyn.rml debug.rml -algorithm.o: algorithm.rml exp.rml types.rml staticexp.rml explode.rml -builtin.o: builtin.rml absyn.rml explode.rml env.rml exp.rml types.rml classinf.rml -classinf.o: classinf.rml absyn.rml -codegen.o: codegen.rml dae.rml dump.rml debug.rml absyn.rml algorithm.rml classinf.rml exp.rml modutil.rml types.rml util.rml -connect.o: connect.rml exp.rml staticexp.rml dae.rml dump.rml -dae.o: dae.rml absyn.rml exp.rml explode.rml algorithm.rml types.rml staticexp.rml rtopts.rml graphviz.rml dump.rml -debug.o: debug.rml rtopts.rml dump.rml -dump.o: dump.rml absyn.rml interactive.rml debug.rml classinf.rml rtopts.rml +absyn.o: absyn.rml debug.rml dump.rml util.rml print.rml +algorithm.o: algorithm.rml exp.rml types.rml explode.rml print.rml +builtin.o: builtin.rml absyn.rml explode.rml env.rml exp.rml types.rml classinf.rml debug.rml print.rml +classinf.o: classinf.rml explode.rml print.rml absyn.rml +classloader.o: classloader.rml absyn.rml system.rml lookup.rml interactive.rml +codegen.o: codegen.rml dae.rml print.rml dump.rml debug.rml absyn.rml algorithm.rml classinf.rml exp.rml modutil.rml types.rml util.rml +connect.o: connect.rml exp.rml staticexp.rml dae.rml dump.rml print.rml +dae.o: dae.rml absyn.rml exp.rml algorithm.rml types.rml rtopts.rml graphviz.rml dump.rml print.rml util.rml +debug.o: debug.rml rtopts.rml dump.rml print.rml +dump.o: dump.rml absyn.rml interactive.rml debug.rml classinf.rml rtopts.rml print.rml util.rml dumpgraphviz.o: dumpgraphviz.rml absyn.rml debug.rml graphviz.rml classinf.rml dump.rml -env.o: env.rml absyn.rml values.rml explode.rml types.rml classinf.rml exp.rml dump.rml graphviz.rml dae.rml -exp.o: exp.rml absyn.rml rtopts.rml util.rml -explode.o: explode.rml absyn.rml dump.rml debug.rml +env.o: env.rml absyn.rml values.rml explode.rml types.rml classinf.rml exp.rml dump.rml graphviz.rml dae.rml print.rml +exp.o: exp.rml absyn.rml rtopts.rml util.rml print.rml +explode.o: explode.rml absyn.rml dump.rml debug.rml print.rml graphviz.o: graphviz.rml -inst.o: inst.rml classinf.rml connect.rml dae.rml debug.rml env.rml exp.rml explode.rml mod.rml prefix.rml types.rml absyn.rml algorithm.rml builtin.rml dump.rml lookup.rml modutil.rml rtopts.rml staticexp.rml values.rml -interactive.o: interactive.rml absyn.rml explode.rml dae.rml -lookup.o: lookup.rml classinf.rml types.rml absyn.rml exp.rml env.rml explode.rml parse.rml debug.rml -main.o: main.rml modutil.rml parse.rml dump.rml dumpgraphviz.rml explode.rml dae.rml inst.rml interactive.rml rtopts.rml debug.rml codegen.rml socket.rml -mod.o: mod.rml absyn.rml dae.rml env.rml exp.rml prefix.rml explode.rml types.rml staticexp.rml values.rml dump.rml -modutil.o: modutil.rml absyn.rml dae.rml exp.rml algorithm.rml rtopts.rml util.rml -prefix.o: prefix.rml absyn.rml exp.rml env.rml lookup.rml util.rml -staticexp.o: staticexp.rml absyn.rml exp.rml explode.rml types.rml env.rml values.rml classinf.rml lookup.rml debug.rml dump.rml -types.o: types.rml classinf.rml absyn.rml exp.rml values.rml explode.rml dump.rml debug.rml +inst.o: inst.rml classinf.rml connect.rml dae.rml debug.rml env.rml exp.rml explode.rml mod.rml prefix.rml types.rml util.rml interactive.rml absyn.rml algorithm.rml builtin.rml dump.rml lookup.rml modutil.rml rtopts.rml staticexp.rml values.rml print.rml +interactive.o: interactive.rml absyn.rml explode.rml dae.rml types.rml values.rml dump.rml debug.rml rtopts.rml util.rml parse.rml prefix.rml mod.rml env.rml lookup.rml classinf.rml exp.rml inst.rml staticexp.rml modutil.rml codegen.rml print.rml system.rml +lookup.o: lookup.rml classinf.rml types.rml absyn.rml exp.rml env.rml explode.rml parse.rml debug.rml dump.rml inst.rml mod.rml prefix.rml print.rml builtin.rml +main.o: main.rml absyn.rml modutil.rml parse.rml dump.rml dumpgraphviz.rml explode.rml dae.rml inst.rml interactive.rml rtopts.rml debug.rml codegen.rml socket.rml print.rml +mod.o: mod.rml absyn.rml dae.rml env.rml exp.rml prefix.rml explode.rml types.rml staticexp.rml values.rml dump.rml print.rml +modutil.o: modutil.rml absyn.rml dae.rml exp.rml algorithm.rml rtopts.rml util.rml print.rml +prefix.o: prefix.rml absyn.rml exp.rml env.rml lookup.rml util.rml print.rml +staticexp.o: staticexp.rml absyn.rml exp.rml explode.rml types.rml env.rml values.rml classinf.rml interactive.rml dump.rml print.rml system.rml lookup.rml debug.rml inst.rml codegen.rml modutil.rml dae.rml +types.o: types.rml classinf.rml absyn.rml exp.rml values.rml explode.rml dump.rml debug.rml print.rml util.o: util.rml -values.o: values.rml +values.o: values.rml exp.rml print.rml system.rml diff --git a/modeq/absyn_builder/walker.g b/modeq/absyn_builder/walker.g index 6816cbb68eb..57db9fca63f 100644 --- a/modeq/absyn_builder/walker.g +++ b/modeq/absyn_builder/walker.g @@ -991,9 +991,10 @@ for_clause_e returns [void* ast] void* id; } : - #(FOR i:IDENT - e = expression - eq = equation_list + + #(FOR #(IN i:IDENT + e = expression ) + eq = algorithm_list ) { id = to_rml_str(i); @@ -1437,8 +1438,13 @@ component_reference__function_call returns [void* ast] ast = Absyn__CREF(cref); } ) - ; - + | + #(INITIAL_FUNCTION_CALL INITIAL ) + { + ast = Absyn__CALL(Absyn__CREF_5fIDENT(mk_scon("initial"), mk_nil()),Absyn__FUNCTIONARGS(mk_nil(),mk_nil())); + } + ; + name_path returns [void* ast] { void* str; diff --git a/modeq/classloader.rml b/modeq/classloader.rml new file mode 100644 index 00000000000..cba2d4a838f --- /dev/null +++ b/modeq/classloader.rml @@ -0,0 +1,217 @@ +(* + Copyright PELAB, Linkoping University + + This file is part of Open Source Modelica (OSM). + + OSM 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 of the License, or + (at your option) any later version. + + OSM 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 Foobar; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*) + +(** + ** file: classloader.rml + ** module: ClassLoader + ** description: Loading of classes from $MODELICAPATH. + ** + ** RCS: $Id$ + ** + ** This module loads classes from $MODELICAPATH. + **) + +module ClassLoader: + with "absyn.rml" + +relation load_class: (Absyn.Path, string) => (Absyn.Program) +end +with "system.rml" +with "lookup.rml" +with "interactive.rml" +with "util.rml" +with "parse.rml" +with "print.rml" +with "env.rml" +with "dump.rml" +(** relation: load_class + ** This relation takes a 'Path' and the $MODELICAPATH as a string + ** and tries to load the class from the path. + ** If the classname is qualified, the complete package is loaded. + ** E.g. load_class(Modelica.SIunits.Voltage) -> whole Modelica package loaded. +**) +relation load_class: (Absyn.Path, string) => (Absyn.Program) = + + (** Simple names: Just load the file if it can be found in $MODELICAPATH* *) + rule load_class_from_mp(classname,mp) => p + ------------------------------------- + load_class(Absyn.IDENT(classname),mp) => p + + (** Qualified names: Load the complete package and then check that **) + (** the package contains the file **) + rule Print.print_buf "Qualified name, loading complete package\n" & + load_complete_package_from_mp(pack,mp,Absyn.TOP,Absyn.PROGRAM([],Absyn.TOP)) => p & + Interactive.get_pathed_class_in_program(path,p)=> _ + ------------------------------------- + load_class(path as Absyn.QUALIFIED(pack,rest),mp) => p + + rule print "load_class failed\n" + ------------------- + load_class(_,_) => fail +end + +relation exist_regular_file: (string) => () = + + rule string_append("test -f ",filename) => cmd & + System.system_call(cmd) => 0 + ---------------------------- + exist_regular_file(filename) +end + +relation exist_directory_file: (string) => () = + + rule string_append("test -d ",filename) => cmd & + System.system_call(cmd) => 0 + ---------------------------- + exist_directory_file(filename) +end + +relation load_class_from_mp: (Absyn.Ident, string) => Absyn.Program = + + rule string_append(class,".mo") => classfile & + Util.string_append_list([mp,"/",classfile])=> classfile' & + exist_regular_file(classfile') & + print "parsing " & print classfile' & print "\n" & + Parser.parse(classfile') => p + --------------------------- + load_class_from_mp(class,mp) => p + + rule Util.string_append_list([mp,"/",class]) => dirfile & + string_append(dirfile,"/package.mo") => packfile & + exist_directory_file dirfile & + exist_regular_file packfile & + Print.print_buf "Class is package stored in a directory, loading whole package(incl. subdir)\n" & + load_complete_package_from_mp(class,mp,Absyn.TOP,Absyn.PROGRAM([],Absyn.TOP)) => p & + Interactive.build_env_from_symboltable(Interactive.SYMBOLTABLE(p,[],[],[],[])) => env & + Lookup.lookup_class(env,Absyn.IDENT(class),true) => (_,_) + -------------------------------------- + load_class_from_mp(class,mp) => p + + rule print "load_class_from_mp failed\n" + -------------------- + load_class_from_mp(_,_) => fail +end + +relation load_complete_package_from_mp: (Absyn.Ident, string, Absyn.Within, Absyn.Program) => Absyn.Program = + + rule Util.string_append_list([mp,"/",pack]) => mp' & + string_append(mp',"/package.mo") => packagefile & + exist_regular_file(packagefile) & + print "parsing " & print packagefile & print "\n" & + Parser.parse(packagefile) => Absyn.PROGRAM(p1,w1) & + Print.print_buf("loading ") & Print.print_buf(packagefile) & Print.print_buf "\n" & + Interactive.update_program(Absyn.PROGRAM(p1,w1),Absyn.PROGRAM(oldc,Absyn.TOP)) => p1' & + System.sub_directories(mp') => subdirs & + Util.string_delimit_list(subdirs, ", ") => subdirstr & + print "subdirs =" & print subdirstr & print "\n" & + load_complete_subdirs(subdirs,pack,mp',within,p1') => p2 & + load_complete_subfiles(pack,mp',within,p2) => p + ------------------------------ + load_complete_package_from_mp(pack,mp,within as Absyn.TOP,Absyn.PROGRAM(oldc,_)) => p + + rule Util.string_append_list([mp,"/",pack]) => mp' & + string_append(mp',"/package.mo") => packagefile & + exist_regular_file(packagefile) & + print "parsing " & print packagefile & print "\n" & + Parser.parse(packagefile) => Absyn.PROGRAM(p1,w1) & + Print.print_buf("loading ") & Print.print_buf(packagefile) & Print.print_buf "\n" & + Interactive.update_program(Absyn.PROGRAM(p1,Absyn.WITHIN(wpath)),Absyn.PROGRAM(oldc,Absyn.TOP)) => p1' & + System.sub_directories(mp') => subdirs & + Util.string_delimit_list(subdirs, ", ") => subdirstr & + print "subdirs =" & print subdirstr & print "\n" & + load_complete_subdirs(subdirs,pack,mp',within,p1') => p2 & + Absyn.join_paths(wpath,Absyn.IDENT(pack))=> wpath' & + load_complete_subfiles(pack,mp',within,p2) => p + ------------------------------ + load_complete_package_from_mp(pack,mp,within as Absyn.WITHIN(wpath),Absyn.PROGRAM(oldc,_)) => p + + axiom load_complete_package_from_mp(_,_,_,_) => fail +end + +relation load_complete_subdirs: (string list, Absyn.Ident, string, + Absyn.Within, Absyn.Program) + => Absyn.Program = + + axiom load_complete_subdirs([],_,_,w,Absyn.PROGRAM(oldcls,w2)) => Absyn.PROGRAM(oldcls,w2) + + rule Absyn.join_paths(pack2,Absyn.IDENT(pack1)) => pack' & + load_complete_package_from_mp(pack,mp,Absyn.WITHIN(pack'),oldp) => p & + load_complete_subdirs(packs,pack1,mp,within,p) => p' + --------------------------------------------- + load_complete_subdirs(pack::packs,pack1,mp,within as Absyn.WITHIN(pack2),oldp) => p' + + rule Absyn.join_paths(Absyn.IDENT(pack1),Absyn.IDENT(pack)) => pack' & + load_complete_package_from_mp(pack,mp,Absyn.WITHIN(Absyn.IDENT(pack1)),oldp) => p & + load_complete_subdirs(packs,pack1,mp,within,p) => p' + --------------------------------------------- + load_complete_subdirs(pack::packs,pack1,mp,within as Absyn.TOP,oldp) => p' + + rule load_complete_subdirs(packs,pack1,mp,within,p) => p' + --------------------------------------------- + load_complete_subdirs(pack::packs,pack1,mp,within,p) => p' + + + rule print "load_complete_subdirs failed\n" + ---------------- + load_complete_subdirs(_,_,_,_,_) => fail +end + +relation load_complete_subfiles: (Absyn.Ident, string, Absyn.Within, Absyn.Program) + => Absyn.Program = + + rule (* Here *.mo files in same directory as package.mo should be loaded as sub-packages*) + System.mo_files(mp)=> mofiles & + Absyn.join_paths(within,Absyn.IDENT(pack)) => within' & + load_subpackage_files(mofiles,mp,Absyn.WITHIN(within'),oldp) => p + ------------------------------------------ + load_complete_subfiles(pack,mp,Absyn.WITHIN(within),oldp) => p + + rule (* Here *.mo files in same directory as package.mo should be loaded as sub-packages*) + System.mo_files(mp)=> mofiles & + load_subpackage_files(mofiles,mp,Absyn.WITHIN(Absyn.IDENT(pack)),oldp) => p + ------------------------------------------ + load_complete_subfiles(pack,mp,Absyn.TOP,oldp) => p + + rule print "load_complete_subfiles failed\n" + ------------------------------------------ + load_complete_subfiles(_,_,_,_) => fail + +end + +relation load_subpackage_files: (string list, string, Absyn.Within, Absyn.Program) + => Absyn.Program = + + axiom load_subpackage_files([],mp,within,Absyn.PROGRAM(cls,w)) => Absyn.PROGRAM(cls,w) + + rule Util.string_append_list([mp,"/",f]) => f' & + print "parsing " & print f' & print "\n" & + Parser.parse(f') => Absyn.PROGRAM(cls,_) & + Print.print_buf("loading ") & Print.print_buf(f') & Print.print_buf "\n" & + Interactive.update_program(Absyn.PROGRAM(cls,within),Absyn.PROGRAM(oldc,Absyn.TOP))=> p' & + load_subpackage_files(fs, mp, within, p') => p'' + ------------------------------- + load_subpackage_files(f::fs,mp,within,Absyn.PROGRAM(oldc,_)) => p'' + + rule print "load_subpackage_files failed\n" + --------------------- + load_subpackage_files(_,_,_,_) => fail +end + diff --git a/modeq/dump.rml b/modeq/dump.rml index 349cba81776..9fc7ab257bd 100644 --- a/modeq/dump.rml +++ b/modeq/dump.rml @@ -68,7 +68,8 @@ with "debug.rml" relation select_string: (bool,string,string) => string relation get_string_list : ('a list, 'a => string, string) => string relation get_option_str: ('a option, 'a => string) => string - relation unparse_annotation_option: Absyn.Annotation option => string + relation unparse_annotation_option: Absyn.Annotation option => string + relation unparse_within: Absyn.Within => string end diff --git a/modeq/inst.rml b/modeq/inst.rml index 1c21065bf7f..37efc9c7164 100644 --- a/modeq/inst.rml +++ b/modeq/inst.rml @@ -199,7 +199,9 @@ relation instantiate_class : (SCode.Program, SCode.Path) => DAE.DAElist = instantiate_class(cdecls, path as Absyn.IDENT(name2)) => DAE.DAE([DAE.COMP(name2,DAE.DAE(dae))]) rule Builtin.initial_env => env & + print "calling inst_class_decls.. \n" & inst_class_decls(env,cdecls,path)=> (env',_) & + print "done \n" & Lookup.lookup_class(env',path,true) => (cdef, env'') & inst_class(env'',Mod.NOMOD,Prefix.NOPRE,[],cdef,[],false) => (dae,_,_,_) & Absyn.path_string path => pathstr diff --git a/modeq/interactive.rml b/modeq/interactive.rml index d7988fa4559..c33b63d70d6 100644 --- a/modeq/interactive.rml +++ b/modeq/interactive.rml @@ -26,7 +26,8 @@ module Interactive: with "explode.rml" with "dae.rml" with "types.rml" - with "values.rml" + with "values.rml" + with "env.rml" datatype InteractiveStmt = IALG of Absyn.AlgorithmItem | IEXP of Absyn.Exp @@ -52,6 +53,8 @@ module Interactive: relation get_component_count: (Absyn.ComponentRef,Absyn.Program) => int relation remove_compiled_functions: (Absyn.Program, (Absyn.Path * Types.Type) list) => (Absyn.Path * Types.Type) list + relation build_env_from_symboltable: (InteractiveSymbolTable) => Env.Env + relation get_pathed_class_in_program: (Absyn.Path, Absyn.Program) => Absyn.Class end with "dump.rml" @@ -61,7 +64,6 @@ with "util.rml" with "parse.rml" with "prefix.rml" with "mod.rml" -with "env.rml" with "lookup.rml" with "classinf.rml" with "exp.rml" @@ -71,6 +73,7 @@ with "modutil.rml" with "codegen.rml" with "print.rml" with "system.rml" +with "classloader.rml" val Placement_records = "record Transformation Real x=0.0; Real y=0.0; Real scale=1.0; Real aspectRatio=1.0; Boolean flipHorizontal=false; Boolean flipVertical=false; Real rotation=0.0; end Transformation; record Placement Boolean visible=true; Transformation transformation; Transformation iconTransformation; end Placement;" @@ -113,6 +116,7 @@ relation evaluate: (InteractiveStmts, InteractiveSymbolTable) => (string,Intera rule SCode.elaborate(p) => p' & Absyn.cref_to_path(cr) => path & + print "elaborated\n" & Inst.instantiate_class(p',path) => dae & DAE.dump_debug dae & Print.print_buf "dumped dae:\n" & @@ -363,10 +367,25 @@ end relation evaluate_graphical_api: (InteractiveStmts, InteractiveSymbolTable) => (string, InteractiveSymbolTable) = - rule let resstr="GAPI function not implemented yet\n" & let newst = st + rule let ISTMTS([IEXP(Absyn.CALL(Absyn.CREF_IDENT("loadClass",_),arg))]) = stmt & + let Absyn.FUNCTIONARGS([ Absyn.CREF(cref) ],[]) = arg & + System.modelicapath => mp & + componentref_to_path(cref) => path & + ClassLoader.load_class(path, mp) => pnew & + update_program(pnew,p) => p' & + Print.get_string => str & + let newst = SYMBOLTABLE(p', s, ic, iv, cf) --------------------------------------------------- - evaluate_graphical_api(ISTMTS([IEXP(Absyn.CALL(Absyn.CREF_IDENT("loadModel",_),_))]),st as SYMBOLTABLE(p,s,ic,iv,cf)) => (resstr,newst) - + evaluate_graphical_api(stmt, st as SYMBOLTABLE(p,s,ic,iv,cf)) => (str,newst) + + rule let ISTMTS([IEXP(Absyn.CALL(Absyn.CREF_IDENT("loadClass",_),arg))]) = stmt & + let Absyn.FUNCTIONARGS([ Absyn.CREF(cref) ],[]) = arg & + componentref_to_path(cref) => path & + Absyn.path_string(path) => pathstr & + Util.string_append_list(["Class ",pathstr," not found in MODELICAPATH.\n"])=> str + --------------------------------------------------- + evaluate_graphical_api(stmt, st as SYMBOLTABLE(p,s,ic,iv,cf)) => (str,st) + rule Parser.parse(name) => p1 & print "Parsed program\n" & update_program(p1,p) => newp @@ -763,6 +782,7 @@ relation evaluate_graphical_api: (InteractiveStmts, InteractiveSymbolTable) => end + relation componentref_to_path: Absyn.ComponentRef => Absyn.Path = axiom componentref_to_path(Absyn.CREF_IDENT(ident,_)) => Absyn.IDENT(ident) @@ -967,9 +987,9 @@ relation update_program: (Absyn.Program,Absyn.Program) => Absyn.Program = axiom update_program (Absyn.PROGRAM([],_),prg) => prg rule class_in_program(name,p2) => false & - update_program(Absyn.PROGRAM(c2,w),Absyn.PROGRAM((c1::c3),w)) => pnew + update_program(Absyn.PROGRAM(c2,w),Absyn.PROGRAM((c1::c3),w2)) => pnew --------------------------------------------------------- - update_program (Absyn.PROGRAM((c1 as Absyn.CLASS(name,_,_,_,_,_))::c2,w as Absyn.TOP),p2 as Absyn.PROGRAM(c3,_)) => pnew + update_program (Absyn.PROGRAM((c1 as Absyn.CLASS(name,_,_,_,_,_))::c2,w as Absyn.TOP),p2 as Absyn.PROGRAM(c3,w2)) => pnew rule class_in_program(name,p2) => true & update_program(Absyn.PROGRAM(c2,w),p2) => pnew & @@ -2221,7 +2241,6 @@ relation insert_class_in_program: (Absyn.Class, Absyn.Within, Absyn.Program) => rule get_class_in_program(n1,p) => c2 & insert_class_in_class(c1,w,c2) => c3 & - update_program(Absyn.PROGRAM([c3],Absyn.TOP),p) => pnew ------------------------------------------------- insert_class_in_program(c1,w as Absyn.WITHIN(Absyn.QUALIFIED(n1,_)),p) => pnew @@ -2232,9 +2251,10 @@ relation insert_class_in_program: (Absyn.Class, Absyn.Within, Absyn.Program) => ------------------------------------------------- insert_class_in_program(c1, w as Absyn.WITHIN(Absyn.IDENT(n1)),p) => pnew - rule Print.print_buf "Error inserting in class, within name, where name is not defined\n" + rule print "Error inserting in class. (" & Dump.unparse_within w => s1 & print s1 + & print ") program = \n" (*& Dump.unparse_str p => pstr & print pstr & print "\n" *) ------------------- - insert_class_in_program(c1,_,p) => fail + insert_class_in_program(c1 as Absyn.CLASS(name,_,_,_,_,_),w,p) => fail end (** relation: insert_class_in_class @@ -2245,7 +2265,7 @@ end **) relation insert_class_in_class: (Absyn.Class, Absyn.Within, Absyn.Class) => Absyn.Class = - rule replace_inner_class(c1,c2) => cnew + rule replace_inner_class(c1,c2) => cnew ---------------------------------- insert_class_in_class(c1,Absyn.WITHIN(Absyn.IDENT(name)),c2) => cnew @@ -2256,7 +2276,7 @@ relation insert_class_in_class: (Absyn.Class, Absyn.Within, Absyn.Class) => Absy --------------------------------------------- insert_class_in_class(c1,Absyn.WITHIN(Absyn.QUALIFIED(name,path)),c2) => cnew' - rule Print.print_buf "insert_class_in_class failed\n" + rule Print.print_buf "insert_class_in_class failed\n" ------------------------------------- insert_class_in_class(_,_,_) => fail end @@ -2300,7 +2320,6 @@ relation replace_class_in_elementitemlist: (Absyn.ElementItem list, Absyn.Class) ------------------------------------------ replace_class_in_elementitemlist((e1 as Absyn.ELEMENTITEM(Absyn.ELEMENT(_,_,_,_,Absyn.CLASSDEF(_,c1 as Absyn.CLASS(name1,_,_,_,_,_)),NONE)))::xs,c as Absyn.CLASS(name,_,_,_,_,_)) => e1::res - rule name1 = name ----------------------------- replace_class_in_elementitemlist( @@ -2308,6 +2327,10 @@ relation replace_class_in_elementitemlist: (Absyn.ElementItem list, Absyn.Class) c2 as Absyn.CLASS(name,_,_,_,_,_)) => Absyn.ELEMENTITEM(Absyn.ELEMENT(a,b,c,d,Absyn.CLASSDEF(e,c2),f))::xs + rule replace_class_in_elementitemlist(xs,c) => res + ----------------------------------------- + replace_class_in_elementitemlist(c1::xs,c) => c1::res + axiom replace_class_in_elementitemlist([],c) => [Absyn.ELEMENTITEM(Absyn.ELEMENT(false,false,Absyn.UNSPECIFIED,"",Absyn.CLASSDEF(false,c),NONE))] end @@ -2321,9 +2344,12 @@ relation get_inner_class: (Absyn.Class, Absyn.Ident) => Absyn.Class = ----------------------------------------------- get_inner_class (Absyn.CLASS(_,_,_,_,_,Absyn.PARTS(parts,_)), name) => c1 - rule Print.print_buf "get_inner_class failed, c:" & - Debug.fcall ("dump", Dump.dump,Absyn.PROGRAM([c],Absyn.TOP)) & - Print.print_buf "name :" & Print.print_buf name + rule Print.get_string => str & + Print.clear_buf & + Print.print_buf "get_inner_class failed, c:" & + Dump.dump Absyn.PROGRAM([c],Absyn.TOP) & + Print.print_buf "name :" & Print.print_buf name & + Print.get_string => s1 & (*print s1 &*) Print.clear_buf & Print.print_buf str -------------- get_inner_class (c,name) => fail end @@ -2361,18 +2387,21 @@ end (** relation: get_public_list - ** This relation takes a ClassPart List and returns the first public ElementItem list of the class. + ** This relation takes a ClassPart List and returns an appended list of all public lists. ** *) relation get_public_list: Absyn.ClassPart list => Absyn.ElementItem list = - - axiom get_public_list Absyn.PUBLIC(lst)::rest => lst + axiom get_public_list([]) => [] + + rule get_public_list(rest) => res2 & + list_append(res1,res2) => res + ----------------------------- + get_public_list Absyn.PUBLIC(res1)::rest => res rule get_public_list(xs) => ys --------------------------- get_public_list(x::xs) => ys - axiom get_public_list(_) => fail end (** relation: get_equation_list @@ -2410,6 +2439,10 @@ relation get_class_from_elementitemlist: (Absyn.ElementItem list, Absyn.Ident) = ------------------------------------------ get_class_from_elementitemlist(Absyn.ELEMENTITEM(Absyn.ELEMENT(_,_,_,_,Absyn.CLASSDEF(_,c1 as Absyn.CLASS(name1,_,_,_,_,_)),NONE))::xs,name) => res + rule get_class_from_elementitemlist(xs,name) => res + ---------------------------------------------- + get_class_from_elementitemlist(_::xs,name) => res + axiom get_class_from_elementitemlist([],_) => fail end diff --git a/modeq/runtime/systemimpl.c b/modeq/runtime/systemimpl.c index 21895a03c58..e2189e6e554 100644 --- a/modeq/runtime/systemimpl.c +++ b/modeq/runtime/systemimpl.c @@ -2,10 +2,12 @@ #include #include #include +#include #include #include #include "read_write.h" #include "../values.h" +#include #ifndef MAXPATHLEN #define MAXPATHLEN 255 @@ -163,6 +165,98 @@ RML_BEGIN_LABEL(System__read_5ffile) } RML_END_LABEL +RML_BEGIN_LABEL(System__modelicapath) +{ + char *path = getenv("MODELICAPATH"); + if (path == NULL) + RML_TAILCALLK(rmlFC); + + rmlA0 = (void*) mk_scon(path); + RML_TAILCALLK(rmlSC); +} +RML_END_LABEL + + +char *select_from_dir; + +int file_select_directories(struct dirent *entry) +{ + char fileName[MAXPATHLEN]; + int res; + struct stat fileStatus; + if ((strcmp(entry->d_name, ".") == 0) || + (strcmp(entry->d_name, "..") == 0)) { + return (0); + } else { + sprintf(fileName,"%s/%s",select_from_dir,entry->d_name); + res = stat(fileName,&fileStatus); + if (res!=0) return 0; + if ((fileStatus.st_mode & _IFDIR)) + return (1); + else + return (0); + } +} + +RML_BEGIN_LABEL(System__sub_5fdirectories) +{ + int i,count; + void *res; + char* directory = RML_STRINGDATA(rmlA0); + if (directory == NULL) + RML_TAILCALLK(rmlFC); + struct dirent **files; + select_from_dir = directory; + count = + scandir(directory, &files, file_select_directories,NULL); + res = mk_nil(); + for (i=0; id_name),res); + } + rmlA0 = (void*) res; + RML_TAILCALLK(rmlSC); +} +RML_END_LABEL + +int file_select_mo(struct dirent *entry) +{ + char fileName[MAXPATHLEN]; + int res; char* ptr; + struct stat fileStatus; + if ((strcmp(entry->d_name, ".") == 0) || + (strcmp(entry->d_name, "..") == 0) || + (strcmp(entry->d_name, "package.mo") == 0)) { + return (0); + } else { + ptr = rindex(entry->d_name, '.'); + if ((ptr != NULL) && + ((strcmp(ptr, ".mo") == 0))) { + return (1); + } else { + return (0); + } + } +} + +RML_BEGIN_LABEL(System__mo_5ffiles) +{ + int i,count; + void *res; + char* directory = RML_STRINGDATA(rmlA0); + if (directory == NULL) + RML_TAILCALLK(rmlFC); + struct dirent **files; + select_from_dir = directory; + count = + scandir(directory, &files, file_select_mo,NULL); + res = mk_nil(); + for (i=0; id_name),res); + } + rmlA0 = (void*) res; + RML_TAILCALLK(rmlSC); +} +RML_END_LABEL RML_BEGIN_LABEL(System__read_5fvalues_5ffrom_5ffile) diff --git a/modeq/system.rml b/modeq/system.rml index 3bd77d5b3de..c45210db146 100644 --- a/modeq/system.rml +++ b/modeq/system.rml @@ -45,4 +45,10 @@ relation cd: (string) => int relation pwd: () => string +relation modelicapath: () => string + +relation sub_directories: (string) => string list + +relation mo_files: (string) => string list + end