Skip to content

Commit

Permalink
Merge pull request #49 from Wopslang/lab
Browse files Browse the repository at this point in the history
v0.1.4
  • Loading branch information
RedoC authored Jan 25, 2022
2 parents 7672e2d + b3b4599 commit 1edc169
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 175 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
- add more function
- support Array(based on `std::vector`)

=== v0.1.4: bf8adbf ===
- improvement
- fixed critical reported bug(#44, #43, #37)
- updated error handler (Now provide the line where the error occured!)
- now Windows user can use the latest Wopslang
- fixed some minor bugs

=== v0.1.3: 88ad93d ===
- WARNING: THIS VERSION IS ONLY AVAILABLE FOR UNIX-TYPE OS.
ALSO, WE STRONGLY RECOMMEND YOU TO UPDATE TO THIS VERSION.
Expand Down Expand Up @@ -36,4 +43,4 @@
- if, for available


2021, Wops Team
2022, Wops Team
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func<WholeNewWorld> Wopslang(contributors) {

## Infomation

Version: **Wopslang v0.1.3 alpha**
Version: **Wopslang v0.1.4 alpha**
License: Apache License 2.0
[⚒️ Official Documentation](./doc/README.md)
[➕ VSCode Extension](https://github.com/Wopslang/vscode-wops)
Expand Down Expand Up @@ -68,4 +68,4 @@ Special Thanks to @jhhan128

![Wopslang Logo](https://emoji.slack-edge.com/T01MFM2TJ07/wopsfull/7fe35e7cbecd2d4d.png)

2021, Wops Team
2022, Wops Team
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ echo -e "\033[92m██║███╗██║██║ ██║██╔═
echo -e "\033[92m╚███╔███╔╝╚██████╔╝██║ ███████║███████╗██║ ██║██║ ╚████║╚██████╔╝\033[m"
echo -e "\033[92m ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚══════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ \033[m"
echo -e "\n"
echo -e "✨ \033[1mWopslang v0.1.3 alpha Builder\033[m"
echo -e "✨ \033[1mWopslang v0.1.4 alpha Builder\033[m"
echo -e "\033[91mWarning: This is alpha version. Some critical issues might be appeared.\033[m"
echo -e -n "- make install..."
make clean
Expand Down
8 changes: 6 additions & 2 deletions src/error/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ enum Err {

class ErrHandler {
public:
void CallErr(std::string errmsg) {
std::cout << "\e[31m" << errmsg << "\e[m";
void CallErr(int error_pos, std::string errmsg) {
if (error_pos != -1) {
std::cout << "\e[31m"<< "line " << error_pos << ": " << errmsg << "\e[m";
exit(1);
}
std::cout << "\e[31m"<< "runtime: " << errmsg << "\e[m";
exit(1);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/import_/eexec_.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ArrayWithCode EExecFunc(std::string func, Array argv) {
void* handle = dlopen("lib/library.so", RTLD_LAZY);

if (!handle) {
ErrHandler().CallErr("Cannot load library: " + std::string(dlerror()));
ErrHandler().CallErr(-1, "Cannot load library: " + std::string(dlerror()));
return {null, ERROR};
}

Expand All @@ -31,7 +31,7 @@ ArrayWithCode EExecFunc(std::string func, Array argv) {
t fptr = (t) dlsym(handle, exfunc.c_str());
const char *dlsym_error = dlerror();
if (dlsym_error) {
ErrHandler().CallErr("Cannot load symbol: " + func);
ErrHandler().CallErr(-1, "Cannot load symbol: " + func);
dlclose(handle);
return {null, ERROR};
}
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(int argc, char **argv) {
if (argc == 2 || (argc == 3 && String(argv[2]) == "debug")) {
std::ifstream handler(String(argv[1]).data());
if (!handler.is_open())
ErrHandler().CallErr("From Interpreter: cannot open the file");
ErrHandler().CallErr(-1, "From Interpreter: cannot open the file");

String codeline;
std::vector<String> code;
Expand All @@ -29,7 +29,7 @@ int main(int argc, char **argv) {

// interpret
std::unordered_map<String, Variable> stor;
AST main(Main, {}, {});
AST main(Main, {}, {}, 0);

if (argc == 3 && String(argv[2]) == "debug") {
std::clock_t end, start = clock();
Expand Down
231 changes: 119 additions & 112 deletions src/parser/parse.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/parser/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
#define WOPS_PARSE_H

void Parse(AST& head, std::vector<String> codes);
Expr ParseExpr(std::vector<std::string> tokens);
Expr ParseExpr(std::vector<std::string> tokens, int parsing_line);

#endif
41 changes: 24 additions & 17 deletions src/runtime/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,20 @@ enum StmtType {
* - bool constant, variable, call (token's type)
* - Variable token
* - std::vector<Expr> children
* - int codeline
*/

class Expr {
private:
bool constant = 0, variable = 0, call = 0;
Variable token;
std::vector<Expr> children;
int codeline;

public:
// constructor
Expr(std::vector<bool> t, Variable tkn) {
token = tkn, constant = t[0], variable = t[1], call = t[2];
Expr(std::vector<bool> t, Variable tkn, int _codeline) {
token = tkn, constant = t[0], variable = t[1], call = t[2], codeline = _codeline;
}

void SetChildren(std::vector<Expr> rplcment) {
Expand All @@ -72,7 +75,7 @@ class Expr {
if (variable) {
auto iter = storage.find(tkn);
if (iter == storage.end()) // has variable declared?
ErrHandler().CallErr(tkn + " has not declared yet");
ErrHandler().CallErr(codeline, tkn + " has not declared yet");
return iter->second;
}
if (constant) {
Expand All @@ -85,7 +88,7 @@ class Expr {
}
ArrayWithCode res = EExecFunc(tkn, arg);
if (res.error == ERROR)
ErrHandler().CallErr("Error occured while calling " + tkn);
ErrHandler().CallErr(codeline, "Error occured while calling " + tkn);

return res.var.container[0];
}
Expand Down Expand Up @@ -151,6 +154,8 @@ typedef std::unordered_map<std::string, Variable> Storage;
* - std::vector<AST *> childStmt
* - std::vector<Expr> expression
* - std::vector<Variable> argument
* public
* - int codeline
*
* expression
*
Expand Down Expand Up @@ -185,9 +190,11 @@ class AST {
std::vector<Variable> argument;

public:
int codeline;

// constructor
AST(StmtType t, std::vector<Variable> argv, std::vector<Expr> expr) {
_t = t, argument = argv, expression = expr;
AST(StmtType t, std::vector<Variable> argv, std::vector<Expr> expr, int _codeline) {
_t = t, argument = argv, expression = expr, codeline = _codeline;
}

void SetChildren(std::vector<AST> rplcment) {
Expand Down Expand Up @@ -226,7 +233,7 @@ class AST {

std::pair<int, bool> res = ast.Execute(storage);
if (res.first >= 1)
ErrHandler().CallErr("break and continue statement only allowed to be used in for statements");
ErrHandler().CallErr(codeline, "break and continue statement only allowed to be used in for statements");

if (res.second) {
ignoreif = 1;
Expand All @@ -239,7 +246,7 @@ class AST {
case ConstDel: {
Variable v_type = argument[0], v_identifier = argument[1];
if (storage.find(v_identifier.GetValue()) != storage.end())
ErrHandler().CallErr("Redefine variable " + v_identifier.GetValue());
ErrHandler().CallErr(codeline, "Redefine variable " + v_identifier.GetValue());

TYPE v_t = v_type.GetValue() == "int" ? INT : (
v_type.GetValue() == "bool" ? BOOL : (
Expand All @@ -262,7 +269,7 @@ class AST {
case VarDel: {
Variable v_type = argument[0], v_identifier = argument[1];
if (storage.find(v_identifier.GetValue()) != storage.end())
ErrHandler().CallErr("Redefine variable " + v_identifier.GetValue());
ErrHandler().CallErr(codeline, "Redefine variable " + v_identifier.GetValue());

TYPE v_t = v_type.GetValue() == "int" ? INT : (
v_type.GetValue() == "bool" ? BOOL : (
Expand All @@ -288,10 +295,10 @@ class AST {
case Assignment: {
Variable v_identifier = argument[0];
if (storage.find(v_identifier.GetValue()) == storage.end())
ErrHandler().CallErr("Variable " + v_identifier.GetValue() + " hasn't defined yet");
ErrHandler().CallErr(codeline, "Variable " + v_identifier.GetValue() + " hasn't defined yet");

if (storage[v_identifier.GetValue()].constant)
ErrHandler().CallErr(v_identifier.GetValue() + " is constant");
ErrHandler().CallErr(codeline, v_identifier.GetValue() + " is constant");

storage[v_identifier.GetValue()].Substitute(expression[0].Execute(storage).GetValue());
break;
Expand All @@ -307,7 +314,7 @@ class AST {
Storage local = storage;
Variable condition = expression[0].Execute(local);
if (condition._t != BOOL)
ErrHandler().CallErr("If Statement allows only boolean condition expression.");
ErrHandler().CallErr(codeline, "If Statement allows only boolean condition expression.");
if (condition.GetValue() == "0") {
return {0, false};
}
Expand All @@ -320,7 +327,7 @@ class AST {
std::pair<int, bool> res = ast.Execute(local);
if (res.first >= 1) {
return {res.first, res.second};
ErrHandler().CallErr("If Statement doesn't allow to use break or continue statement.");
ErrHandler().CallErr(codeline, "If Statement doesn't allow to use break or continue statement.");
}
if (res.second) {
ignoreif = 1;
Expand All @@ -340,7 +347,7 @@ class AST {
Storage local = storage;
Variable condition = expression[0].Execute(local);
if (condition._t != BOOL)
ErrHandler().CallErr("Elif Statement allows only boolean condition expression.");
ErrHandler().CallErr(codeline, "Elif Statement allows only boolean condition expression.");
if (condition.GetValue() == "0") {
return {0, false};
}
Expand All @@ -353,7 +360,7 @@ class AST {
std::pair<int, bool> res = ast.Execute(local);
if (res.first >= 1) {
return {res.first, res.second};
ErrHandler().CallErr("Elif Statement doesn't allow to use break or continue statement.");
ErrHandler().CallErr(codeline, "Elif Statement doesn't allow to use break or continue statement.");
}
if (res.second) {
ignoreif = 1;
Expand All @@ -380,7 +387,7 @@ class AST {
std::pair<int, bool> res = ast.Execute(local);
if (res.first >= 1) {
return {res.first, res.second};
ErrHandler().CallErr("Else Statement doesn't allow to use break or continue statement.");
ErrHandler().CallErr(codeline, "Else Statement doesn't allow to use break or continue statement.");
}
if (res.second) {
ignoreif = 1;
Expand Down Expand Up @@ -434,7 +441,7 @@ class AST {
while (1) {
Variable condition = expression[0].Execute(local);
if (condition._t != BOOL)
ErrHandler().CallErr("For Statement allows only boolean condition expression.");
ErrHandler().CallErr(codeline, "For Statement allows only boolean condition expression.");
if (condition.GetValue() == "0") break;

bool ignoreif = 0;
Expand Down
Loading

0 comments on commit 1edc169

Please sign in to comment.