Skip to content

Commit

Permalink
Add input
Browse files Browse the repository at this point in the history
add input
  • Loading branch information
sriharivishnu committed Oct 10, 2020
2 parents 4f4900d + 8eccc70 commit b9e53ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
5 changes: 3 additions & 2 deletions examples/fibonacci.nov
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fun getFibonacci(n) {
}
return arr;
};

print (getFibonacci(10));
print("Please enter a valid integer: ");
var n = getnum();
print (getFibonacci(n));

3 changes: 2 additions & 1 deletion src/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ int main(int arg_count, char * arg_list[]) {
myfile.close();
} else {
std::cout << "Could not open file " << arg_list[1] << std::endl;
return 1;
}
nova::run(toRun, global, false);
nova::run(toRun, global, false, arg_list[1]);
}
return 0;
}
18 changes: 18 additions & 0 deletions src/stdfuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ void addStdFunctions(const Context& context) {
context.symbols->addFunction("str", [&](std::vector<shared_obj> arg) {
return arg[0]->toString();
}, 1);
context.symbols->addFunction("input", [&](std::vector<shared_obj> arg) {
std::string line;
std::getline(std::cin, line);
return line;
}, 0);
context.symbols->addFunction("getnum", [&](std::vector<shared_obj> arg) {
int num;
if (!scanf("%d", &num)) {
throw Error("Conversion Exception", "Cannot convert string to integer");
};
return num;
}, 0);
context.symbols->addFunction("getword", [&](std::vector<shared_obj> arg) {
std::string s;
std::cin >> s;
return s;
}, 0);

context.symbols->addFunction("int", [&](std::vector<shared_obj> arg) {
if (arg[0]->value.isType<std::string>()) {
try {
Expand Down

0 comments on commit b9e53ae

Please sign in to comment.