Advertisement
Guest User

Heil

a guest
Feb 28th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include "ast.hpp"
  2.  
  3. #include <regex>
  4.  
  5. int32_t Interpret(
  6.     InterpretContext &context, // Contains the parameters and variable bindings
  7.     TreePtr program
  8. ){
  9.     std::regex reNum("^-?[0-9]+$");
  10.     std::regex reId("^[a-z][a-z 0-9]*$");
  11.    
  12.     if( regex_match(program->type, reNum) ){
  13.         return std::atol(program->type.c_str());
  14.        
  15.     // TODO : Check for things matching reId
  16.     }
  17.     else if( regex_match(program->type, reId) ){
  18.         return context.bindings[program->type.c_str()];
  19.        
  20.     }
  21.     else if(program->type=="Param"){
  22.         unsigned index=atol(program->value.c_str());
  23.         auto value=context.params.at(index);
  24.         return value;
  25.        
  26.     }
  27.     else if(program->type=="Assign"){ //Works
  28.         int32_t val = Interpret(context, program->branches.at(0));
  29.         context.bindings[program->value.c_str()] = val;
  30.         return val;
  31.     }
  32.     else if(program->type=="Input"){
  33.         int32_t val;
  34.         std::cin >> val;
  35.         return val;
  36.     }
  37.     else if(program->type=="Output"){
  38.         int32_t val=Interpret(context, program->branches.at(0));
  39.         std::cout<<val<<std::endl;
  40.         return val;
  41.        
  42.     // TODO: Handle other constructs
  43.        
  44.     }
  45.     else if(program->type=="While"){
  46.         while(Interpret(context, program->branches.at(0)) != 0){
  47.             Interpret(context, program->branches.at(1));
  48.         }
  49.         return 0;
  50.     }
  51.     else if(program->type=="Seq"){ //Works
  52.         int32_t val;
  53.         int vecsize = program->branches.size();
  54.         for(int i = 0; i < vecsize; i++){
  55.             val = Interpret(context, program->branches.at(i));
  56.         }
  57.         return val;
  58.     }
  59.     else if(program->type=="Add"){
  60.         int32_t leftval = Interpret(context, program->branches.at(0));
  61.         int32_t rightval = Interpret(context, program->branches.at(1));
  62.         int32_t val = leftval + rightval;
  63.         return val;
  64.     }
  65.     else if(program->type=="Sub"){
  66.         int32_t leftval = Interpret(context, program->branches.at(0));
  67.         int32_t rightval = Interpret(context, program->branches.at(1));
  68.         int32_t val = leftval - rightval;
  69.         return val;
  70.     }
  71.     else if(program->type=="If"){
  72.         int32_t val;
  73.         if(Interpret(context, program->branches.at(0)) != 0){
  74.             val = Interpret(context, program->branches.at(1));
  75.         }
  76.         else {
  77.             val = Interpret(context, program->branches.at(2));
  78.         }
  79.         return val;
  80.     }
  81.     else if(program->type=="LessThan"){
  82.         int32_t val;
  83.         if(Interpret(context, program->branches.at(0)) < Interpret(context, program->branches.at(1))){
  84.             return 1;
  85.         }
  86.         else {
  87.             return 0;
  88.         }
  89.     }
  90.     else{
  91.         throw std::runtime_error("Unknown construct '"+program->type+"'");
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement