Parcourir la source

Add execution of a file worksheet

Jean Fromentin il y a 8 ans
Parent
commit
72887eeb79
9 fichiers modifiés avec 43 ajouts et 9 suppressions
  1. 2 0
      .gitignore
  2. 1 1
      ext/base/init.cpp
  3. 23 1
      ext/base/kernel.cpp
  4. 3 0
      ext/base/kernel.hpp
  5. 4 3
      interpreter.cpp
  6. 6 2
      interpreter.hpp
  7. 1 1
      kernel.cpp
  8. 1 1
      main.cpp
  9. 2 0
      worksheet/test

+ 2 - 0
.gitignore

@@ -1,3 +1,5 @@
+gomu.dSYM*
+
 #Documentation
 doc/*
 

+ 1 - 1
ext/base/init.cpp

@@ -42,7 +42,7 @@ extern "C"{
   //--- Contextual functions ---//
   Gomu::Module::Function contextual_functions[]={
     {"Void","delete",{"Symbol"},(void*)del},
-    //{"Void","load",{"String"},(void*)load},
+    {"Void","execute",{"String"},(void*)execute},
     {"Generic","operator=",{"Symbol","Generic"},(void*)assignment},
     {"Array","symbols",{"Type"},(void*)symbols},
     {"Type","type",{"Generic"},(void*)type},

+ 23 - 1
ext/base/kernel.cpp

@@ -19,6 +19,9 @@
 
 #include "kernel.hpp"
 #include "../../interpreter.hpp"
+#include <fstream>
+
+
 
 //------------------------------------
 // assignment(Context&,Value&,Value&)
@@ -55,6 +58,24 @@ Value del(Context&,Value& v){
   return Value(type_void,nullptr);
 }
 
+//--------------------------
+// execute(Context&,Value&)
+//--------------------------
+
+Value execute(Context& context,Value& file){
+  const string &filename="worksheet/"+*((string*)file.ptr);
+  ifstream fs;
+  fs.open(filename.c_str(),fstream::in);
+  string cmd;
+  size_t line=1;
+  while(getline(fs,cmd)){
+    context.interpreter->eval(cmd,context,false);
+  }
+  fs.close();
+  return Value(type_void,nullptr);
+
+}
+
 //---------
 // symbols
 //---------
@@ -72,7 +93,7 @@ Value symbols(Context& context,Value& v){
   res->type=type_string;
   for(size_t i=0;i<stack.size();++i){
     res->tab[i]=new string(stack[i]);
-  }
+    }
   return Value(type_array,res);
 }
 
@@ -85,3 +106,4 @@ Value type(Context&,Value& v){
   else return Value(type_type,v.type);
 }
 
+

+ 3 - 0
ext/base/kernel.hpp

@@ -34,3 +34,6 @@ Value type(Context&,Value&);
 
 //! Return an array of member functions of a type
 Value symbols(Context&,Value&);
+
+//! Execute commands strored in a file
+Value execute(Context&, Value&);

+ 4 - 3
interpreter.cpp

@@ -49,7 +49,8 @@ namespace Gomu{
   // Context::Context()
   //--------------------
   
-  Context::Context(){
+  Context::Context(Interpreter* inter){
+    interpreter=inter;
     add_symbol("context",type_context,this)->hide=true;
     add_symbol("Array",type_type,type_array);
     add_symbol("Boolean",type_type,type_boolean)->hide=true;
@@ -1116,7 +1117,7 @@ namespace Gomu{
   // Interpreter::eval(string)
   //---------------------------
   
-  void Interpreter::eval(string cmd,Context& context){
+  void Interpreter::eval(string cmd,Context& context,bool display){
     size_t root;
     Node* node;
     bool error=false;
@@ -1127,7 +1128,7 @@ namespace Gomu{
       eval_expression(root,context);
       node=&nodes[root];
       Value* value=node->value.eval();
-      if(value->type!=nullptr and value->type!=type_void){
+      if(value->type!=nullptr and value->type!=type_void and display){
 	cout<<value->disp()<<endl;
       }
     }

+ 6 - 2
interpreter.hpp

@@ -146,11 +146,14 @@ namespace Gomu{
   //! A class for the interpretation context
   class Context{
   public:
+    //! Pointer to an interpreter
+    Interpreter* interpreter;
+    
     //! Map for association name <-> symbol
     map<string,Symbol> symbols;
     
     //! The unique constructor
-    Context();
+    Context(Interpreter* interpreter);
 
     //! Add a symbol for a contextual function
     //! \param name name of the symbol
@@ -474,7 +477,8 @@ namespace Gomu{
     //! Evaluate a command
     //! \param cmd command to evaluate
     //! \param context context of the evaluation
-    void eval(string cmd,Context& context);
+    //! \param display specify if we display the last valus
+    void eval(string cmd,Context& context,bool display=true);
 
     //! Evaluate an expression
     //! \param pos indice of the expression to evaluate

+ 1 - 1
kernel.cpp

@@ -348,7 +348,7 @@ namespace Gomu{
     interpreter.add_operator("*","operator*",opBinary,3);
     interpreter.add_operator("::","operator::",opBinary,1);
     interpreter.add_operator("!","factorial",opPostUnitary,2);
-    context.add_contextual_function("Void","load_module",{"String"},(void*)load_module);
+    context.add_contextual_function("Void","load",{"String"},(void*)load_module);
     context.add_contextual_function("Void","unload",{"Module"},(void*)unload);
     context.add_contextual_function("Void","reload",{"Module"},(void*)reload);
   }

+ 1 - 1
main.cpp

@@ -42,8 +42,8 @@ static char* completion_generator(const char* str,int state);
 
 //! Main function
 int main(){
-  Context context;
   Interpreter interpreter;
+  Context context(&interpreter);
   completion_context=&context;
   completion_interpreter=&interpreter;
   try{

+ 2 - 0
worksheet/test

@@ -0,0 +1,2 @@
+a="Hello world"
+l=a.len()