diff options
author | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2013-01-01 23:38:10 +0100 |
---|---|---|
committer | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2013-01-01 23:38:10 +0100 |
commit | 0992a3a67db500c2ed88218e47043482ab3829ac (patch) | |
tree | f513fe2ffb79638a11bae505aaa01b63e23e08e8 | |
parent | 7e4ac66f3446f4dc5b871a047400265f0c046952 (diff) | |
download | sencha-lang-0992a3a67db500c2ed88218e47043482ab3829ac.tar.gz sencha-lang-0992a3a67db500c2ed88218e47043482ab3829ac.tar.bz2 sencha-lang-0992a3a67db500c2ed88218e47043482ab3829ac.tar.xz sencha-lang-0992a3a67db500c2ed88218e47043482ab3829ac.zip |
Bug with passing variables to funcitons resolved.
-rw-r--r-- | Sencha-lang/AST/VariableExpression.cpp | 6 | ||||
-rw-r--r-- | Sencha-lang/ContextManager.cpp | 12 | ||||
-rw-r--r-- | Sencha-lang/Examples/example1.se | 2 | ||||
-rw-r--r-- | Sencha-lang/Examples/example1_output | 2 | ||||
-rw-r--r-- | Sencha-lang/Examples/example2.se | 8 | ||||
-rw-r--r-- | Sencha-lang/Examples/example2_output | 1 | ||||
-rw-r--r-- | Sencha-lang/Examples/example3.se | 8 | ||||
-rw-r--r-- | Sencha-lang/Examples/example3_output | 101 | ||||
-rw-r--r-- | Sencha-lang/Examples/example4.se | 11 | ||||
-rw-r--r-- | Sencha-lang/Examples/example4_output | 10 | ||||
-rw-r--r-- | Sencha-lang/Examples/example5.se | 17 | ||||
-rw-r--r-- | Sencha-lang/Lexer.cpp | 3 | ||||
-rw-r--r-- | Sencha-lang/Parser.cpp | 20 | ||||
-rw-r--r-- | Sencha-lang/Parser.h | 3 | ||||
-rw-r--r-- | Sencha-lang/main.cpp | 6 |
15 files changed, 196 insertions, 14 deletions
diff --git a/Sencha-lang/AST/VariableExpression.cpp b/Sencha-lang/AST/VariableExpression.cpp index 895e131..22f4465 100644 --- a/Sencha-lang/AST/VariableExpression.cpp +++ b/Sencha-lang/AST/VariableExpression.cpp @@ -20,15 +20,17 @@ VariableExpression::~VariableExpression() { SenchaObject VariableExpression::evaluate() { + auto context = context_manager->get_top(); + //std::cout << "I'm retrieving variable " + name + " from " + context->name << std::endl; SenchaObject result = context->get(name); + //std::cout << "And it's: " + result.repr() << std::endl; result.name = name; return result; } SenchaObject VariableExpression::execute() { - SenchaObject result = evaluate(); - return result; + return evaluate(); } diff --git a/Sencha-lang/ContextManager.cpp b/Sencha-lang/ContextManager.cpp index 51993de..74a3ac0 100644 --- a/Sencha-lang/ContextManager.cpp +++ b/Sencha-lang/ContextManager.cpp @@ -35,6 +35,10 @@ SenchaObject ContextManager::execute_function(std::string name, std::vector<ASTE else if(contexts["global"]->registered_sfunctions.count(name) == 1) { + std::vector<SenchaObject> evaluated_arguments; + for(auto argument : arguments) + evaluated_arguments.push_back((argument->evaluate())); + SenchaFunction * function = contexts["global"]->registered_sfunctions[name]; std::string name_of_context = create_new_context()->name; if( arguments.size() != function->names_of_arguments.size()) @@ -42,9 +46,13 @@ SenchaObject ContextManager::execute_function(std::string name, std::vector<ASTE result.type = SenchaObject::invalid; return result; } - for(unsigned int i = 0; i < function->names_of_arguments.size(); i++) - get_top()->add(function->names_of_arguments[i], arguments[i]->evaluate()); + { + //std::cout << "I'm adding to context " + get_top()->name + " variable of name: " + function->names_of_arguments[i]; + //std::cout << "Which should be equal " + evaluated_arguments[i].repr() << std::endl; + get_top()->add(function->names_of_arguments[i], evaluated_arguments[i]); + } + result = (*function)(); pop_context(); destroy_context(name_of_context); diff --git a/Sencha-lang/Examples/example1.se b/Sencha-lang/Examples/example1.se new file mode 100644 index 0000000..6972da8 --- /dev/null +++ b/Sencha-lang/Examples/example1.se @@ -0,0 +1,2 @@ +if(true) print("I like bananas"); +print(1*77-3); diff --git a/Sencha-lang/Examples/example1_output b/Sencha-lang/Examples/example1_output new file mode 100644 index 0000000..ea8def4 --- /dev/null +++ b/Sencha-lang/Examples/example1_output @@ -0,0 +1,2 @@ +I like bananas +74 diff --git a/Sencha-lang/Examples/example2.se b/Sencha-lang/Examples/example2.se new file mode 100644 index 0000000..5b6afbd --- /dev/null +++ b/Sencha-lang/Examples/example2.se @@ -0,0 +1,8 @@ + x = 9; + if( x != 9) { + print("dda"); + } else if (x == 11) { + print("Lubie banany"); + } else { + print(9); + } diff --git a/Sencha-lang/Examples/example2_output b/Sencha-lang/Examples/example2_output new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/Sencha-lang/Examples/example2_output @@ -0,0 +1 @@ +9 diff --git a/Sencha-lang/Examples/example3.se b/Sencha-lang/Examples/example3.se new file mode 100644 index 0000000..518fefe --- /dev/null +++ b/Sencha-lang/Examples/example3.se @@ -0,0 +1,8 @@ +x = 1; +repeat(100) +{ + print("sin(x) x= ", x, " ", sin(x)); + x = x +1; +} + +if(x == 101) print("Hurray, success!"); diff --git a/Sencha-lang/Examples/example3_output b/Sencha-lang/Examples/example3_output new file mode 100644 index 0000000..36382df --- /dev/null +++ b/Sencha-lang/Examples/example3_output @@ -0,0 +1,101 @@ +sin(x) x= 1 0.841471 +sin(x) x= 2 0.909297 +sin(x) x= 3 0.14112 +sin(x) x= 4 -0.756802 +sin(x) x= 5 -0.958924 +sin(x) x= 6 -0.279415 +sin(x) x= 7 0.656987 +sin(x) x= 8 0.989358 +sin(x) x= 9 0.412118 +sin(x) x= 10 -0.544021 +sin(x) x= 11 -0.99999 +sin(x) x= 12 -0.536573 +sin(x) x= 13 0.420167 +sin(x) x= 14 0.990607 +sin(x) x= 15 0.650288 +sin(x) x= 16 -0.287903 +sin(x) x= 17 -0.961397 +sin(x) x= 18 -0.750987 +sin(x) x= 19 0.149877 +sin(x) x= 20 0.912945 +sin(x) x= 21 0.836656 +sin(x) x= 22 -0.00885131 +sin(x) x= 23 -0.84622 +sin(x) x= 24 -0.905578 +sin(x) x= 25 -0.132352 +sin(x) x= 26 0.762558 +sin(x) x= 27 0.956376 +sin(x) x= 28 0.270906 +sin(x) x= 29 -0.663634 +sin(x) x= 30 -0.988032 +sin(x) x= 31 -0.404038 +sin(x) x= 32 0.551427 +sin(x) x= 33 0.999912 +sin(x) x= 34 0.529083 +sin(x) x= 35 -0.428183 +sin(x) x= 36 -0.991779 +sin(x) x= 37 -0.643538 +sin(x) x= 38 0.296369 +sin(x) x= 39 0.963795 +sin(x) x= 40 0.745113 +sin(x) x= 41 -0.158623 +sin(x) x= 42 -0.916522 +sin(x) x= 43 -0.831775 +sin(x) x= 44 0.0177019 +sin(x) x= 45 0.850904 +sin(x) x= 46 0.901788 +sin(x) x= 47 0.123573 +sin(x) x= 48 -0.768255 +sin(x) x= 49 -0.953753 +sin(x) x= 50 -0.262375 +sin(x) x= 51 0.670229 +sin(x) x= 52 0.986628 +sin(x) x= 53 0.395925 +sin(x) x= 54 -0.558789 +sin(x) x= 55 -0.999755 +sin(x) x= 56 -0.521551 +sin(x) x= 57 0.436165 +sin(x) x= 58 0.992873 +sin(x) x= 59 0.636738 +sin(x) x= 60 -0.304811 +sin(x) x= 61 -0.966118 +sin(x) x= 62 -0.739181 +sin(x) x= 63 0.167356 +sin(x) x= 64 0.920026 +sin(x) x= 65 0.826829 +sin(x) x= 66 -0.0265512 +sin(x) x= 67 -0.85552 +sin(x) x= 68 -0.897928 +sin(x) x= 69 -0.114785 +sin(x) x= 70 0.773891 +sin(x) x= 71 0.951055 +sin(x) x= 72 0.253823 +sin(x) x= 73 -0.676772 +sin(x) x= 74 -0.985146 +sin(x) x= 75 -0.387782 +sin(x) x= 76 0.566108 +sin(x) x= 77 0.99952 +sin(x) x= 78 0.513978 +sin(x) x= 79 -0.444113 +sin(x) x= 80 -0.993889 +sin(x) x= 81 -0.629888 +sin(x) x= 82 0.313229 +sin(x) x= 83 0.968364 +sin(x) x= 84 0.73319 +sin(x) x= 85 -0.176076 +sin(x) x= 86 -0.923458 +sin(x) x= 87 -0.821818 +sin(x) x= 88 0.0353983 +sin(x) x= 89 0.860069 +sin(x) x= 90 0.893997 +sin(x) x= 91 0.105988 +sin(x) x= 92 -0.779466 +sin(x) x= 93 -0.948282 +sin(x) x= 94 -0.245252 +sin(x) x= 95 0.683262 +sin(x) x= 96 0.983588 +sin(x) x= 97 0.379608 +sin(x) x= 98 -0.573382 +sin(x) x= 99 -0.999207 +sin(x) x= 100 -0.506366 +Hurray, success! diff --git a/Sencha-lang/Examples/example4.se b/Sencha-lang/Examples/example4.se new file mode 100644 index 0000000..b050465 --- /dev/null +++ b/Sencha-lang/Examples/example4.se @@ -0,0 +1,11 @@ +y = "I like bananas"; +x = 10; +while(x != 20) +{ + if(x /2 == 6) + { + print("Oh, it is close to 12 ", x); + } + else print("It is not 12"); + x = x +1; +} diff --git a/Sencha-lang/Examples/example4_output b/Sencha-lang/Examples/example4_output new file mode 100644 index 0000000..d9bf0e0 --- /dev/null +++ b/Sencha-lang/Examples/example4_output @@ -0,0 +1,10 @@ +It is not 12 +It is not 12 +Oh, it is close to 12 12 +Oh, it is close to 12 13 +It is not 12 +It is not 12 +It is not 12 +It is not 12 +It is not 12 +It is not 12 diff --git a/Sencha-lang/Examples/example5.se b/Sencha-lang/Examples/example5.se new file mode 100644 index 0000000..48707f5 --- /dev/null +++ b/Sencha-lang/Examples/example5.se @@ -0,0 +1,17 @@ +def compute(old, new, time) +{ + return (new - old)/ time; +} + +def easy(old) { + return old - 1; +} + +old = 100; +new = 120; +time = 4; + +println("Old was: ", old, " new is: ", new, " and it changed in: ", time, " seconds" ); + +println(easy(time)); +println(compute(old, new, time));
\ No newline at end of file diff --git a/Sencha-lang/Lexer.cpp b/Sencha-lang/Lexer.cpp index 7d41a75..52c5af6 100644 --- a/Sencha-lang/Lexer.cpp +++ b/Sencha-lang/Lexer.cpp @@ -41,7 +41,6 @@ std::string Lexer::unescape_string(string text) replacement = 27;
break;
}
-
}
if(replacement != 0)
{
@@ -52,8 +51,6 @@ std::string Lexer::unescape_string(string text) result.replace(i + offset, 2, replacement_chars);
offset--;
}
-
-
}
return result;
}
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp index 157f59a..81c8d84 100644 --- a/Sencha-lang/Parser.cpp +++ b/Sencha-lang/Parser.cpp @@ -74,6 +74,16 @@ void Parser::interpret() bool Parser::peek(string s) {return tok_value == s;} +bool Parser::peek_one_ahead(string s) +{ + if(position_in_stream < token_stream.size() - 1) + { + if(s == token_stream[position_in_stream].value) + return true; + } + return false; +} + bool Parser::accept(string s) { if(peek(s)) @@ -251,11 +261,13 @@ ASTExpression * Parser::prim_expr() ASTExpression * Parser::postfix_expr() {
string name = tok_value;
- if(is_function_name())
+ if(current_token.get_type() == t_symbol)
{
- PostfixExpression * function_call = new PostfixExpression(name, context_manager);
- if(accept("("))
- {
+ if(peek_one_ahead("("))
+ { + read_next(); + read_next(); + PostfixExpression * function_call = new PostfixExpression(name, context_manager);
if(!accept(")"))
{
function_call->add_argument(expr());
diff --git a/Sencha-lang/Parser.h b/Sencha-lang/Parser.h index d1c5d7c..849e816 100644 --- a/Sencha-lang/Parser.h +++ b/Sencha-lang/Parser.h @@ -36,7 +36,8 @@ class Parser bool in_statement; bool read_next(); - bool peek(string s); + bool peek(string s);
+ bool peek_one_ahead(string s); bool accept(string s); bool expect(string s); bool is_type(); diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp index 0854c21..b5283d0 100644 --- a/Sencha-lang/main.cpp +++ b/Sencha-lang/main.cpp @@ -200,7 +200,7 @@ void interactive() parser.program->execute_last(); inspector.visit(parser.program); //cout << parser.show_tokens(); - cout << inspector.get_report(); + //cout << inspector.get_report(); inspector.forget_everything(); } } @@ -249,9 +249,11 @@ int main(int argc, char *argv[]) } input_file.close(); } - + ASTInspector inspector; parser.interpret(); + inspector.visit(parser.program); parser.program->execute(); + //cout << inspector.get_report()<< endl; } } |