forked from LangProc/langproc-2017-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_interpret.cpp
32 lines (24 loc) · 906 Bytes
/
ast_interpret.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "ast.hpp"
#include <regex>
int32_t Interpret(
InterpretContext &context, // Contains the parameters and variable bindings
TreePtr program
){
std::regex reNum("^-?[0-9]+$");
std::regex reId("^[a-z][a-z0-9]*$");
if( regex_match(program->type, reNum) ){
return std::atol(program->type.c_str());
// TODO : Check for things matching reId
}else if(program->type=="Param"){
unsigned index=atol(program->value.c_str());
auto value=context.params.at(index);
return value;
}else if(program->type=="Output"){
int32_t val=Interpret(context, program->branches.at(0));
std::cout<<val<<std::endl;
return val;
// TODO: Handle other constructs
}else{
throw std::runtime_error("Unknown construct '"+program->type+"'");
}
}