Skip to content

Commit

Permalink
Merge pull request #30 from Wopslang/lab
Browse files Browse the repository at this point in the history
v0.1.2
  • Loading branch information
redocmath committed Oct 18, 2021
2 parents 434f54f + 582b470 commit 259b873
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
- add more function
- support Array(based on `std::vector`)

=== v0.1.2: 4bf6081 ===

- feature
- fixed type error
- add debug mode
- add negative and positive number;
- and fixed some minor bugs

=== v0.1.1: cdc54df ===

- feature
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func<WholeNewWorld> Wopslang(contributors) {

## Infomation

Version: **Wopslang v0.1.1 alpha**
Version: **Wopslang v0.1.2 alpha**
License: Apache License 2.0
[⚒️ Official Documentation](./doc/README.md)

Expand Down
9 changes: 9 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== TODO-LIST ===
1: Array
- Declare: int a[1000];
- Declare with initialization: int a[5] <- {1 ~ 5} or int a[5] <- {1, 2, 3, 4, 5}
- Substitution: a[3] = 7
- Substitution with range: a[0 ~ 3] = 100
- For statement with Array: for i in a
2: Error System
- Print the line number where the error was occured
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.1 alpha Builder\033[m"
echo -e "✨ \033[1mWopslang v0.1.2 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
Expand Down
2 changes: 1 addition & 1 deletion doc/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ binary_op = "||" | "&&" | rel_op | add_op | mul_op .
rel_op = "==" | "!=" | "<" | "<=" | ">" | ">=" .
add_op = "+" | "-" .
mul_op = "*" | "/" | "%" .
unary_op = "!" .
unary_op = "!" | "+" | "-" .
```

Each operator has a different priority for parsing. For instance, unary operators have the highest priority.
Expand Down
Binary file modified src/import_/dll/library.so
Binary file not shown.
12 changes: 11 additions & 1 deletion src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
* */

#include <fstream>
#include <ctime>
#include "runtime/AST.h"
#include "parser/parse.h"

int main(int argc, char **argv) {
std::cout << "\e[31m" << "Warning: This is alpha version. Some critical issues might be appeared." << "\e[m\n";
if (argc == 2) {
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");
Expand All @@ -29,6 +30,15 @@ int main(int argc, char **argv) {
// interpret
std::unordered_map<String, Variable> stor;
AST main(Main, {}, {});

if (argc == 3 && String(argv[2]) == "debug") {
std::clock_t end, start = clock();
Parse(main, code);
main.Execute(stor);
end = clock();
std::cout << "\n=== DEBUG ===\n\e[32m" << "Running Time(ms): " << (double)(end-start) * 1000 / CLOCKS_PER_SEC << "\e[m\n";
return 0;
}
Parse(main, code);
main.Execute(stor);
} else {
Expand Down
42 changes: 38 additions & 4 deletions src/parser/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ std::vector<char> oprs{
'+', '-', '*', '/', '%', '=', '>', '<', '!', '&', '|', '(', ')', '[', ']', '{', '}', ',',
};

std::vector<String> operators{
"+", "-", "*", "/", "%", "=", "!=", ">", "<", ">=", "<=", "!", "&&", "||"
};

std::vector<std::pair<String, String>> runes{
{"\a", "a"}, {"\b", "b"}, {"\f", "f"}, {"\n", "n"}, {"\r", "r"}, {"\t", "t"}, {"\v", "v"}, {"\\", "\\"}, {"\'", "'"}, {"\"", "\""}
};

std::vector<String> funcs {"in", "out", "tostring", "toint"};

Expr ParseExpr(std::vector<String> tokens) {
Expr head({0,0,0}, Variable("_", "", INT));
Expr head({0,0,0}, Variable("_", "", OPERATOR));

if (tokens.size() >= 3 && std::find(funcs.begin(), funcs.end(), tokens[0]) != funcs.end() &&
tokens[1] == "(" && tokens[tokens.size()-1] == ")") {
Expand Down Expand Up @@ -64,7 +68,7 @@ Expr ParseExpr(std::vector<String> tokens) {
if (tokens.size() == 1) {
if (std::regex_match(tokens[0], std::regex("[0-9]+"))) {
head = Expr({1, 0, 0}, Variable("_", tokens[0], INT));
} else if (std::regex_match(tokens[0], std::regex("[0-9]+.[0-9]"))) {
} else if (std::regex_match(tokens[0], std::regex("[0-9]+.[0-9]+"))) {
head = Expr({1, 0, 0}, Variable("_", tokens[0], DOUBLE));
} else if (tokens[0][0] == '\"' && tokens[0][tokens[0].length()-1] == '\"') {
head = Expr({1, 0, 0}, Variable("_", tokens[0], STRING));
Expand Down Expand Up @@ -179,7 +183,22 @@ Expr ParseExpr(std::vector<String> tokens) {
String token = tokens[idx];
if (!isTarget[idx]) continue;
if (token == "+") {
if (idx == 0 || idx == tokens.size()-1) ErrHandler().CallErr("operator + cannot be unary");
if (idx != 0 && std::find(operators.begin(), operators.end(), tokens[idx - 1]) != operators.end())
continue;
if (idx == tokens.size()-1) ErrHandler().CallErr("operator + cannot be unary");
if (idx == 0) {
if (tokens.size() != 2) ErrHandler().CallErr("invalid unary operation form");
if (std::regex_match(tokens[1], std::regex("[0-9]+"))) {
head = Expr({1, 0, 0}, Variable("_", tokens[1], INT));
} else if (std::regex_match(tokens[1], std::regex("[0-9]+.[0-9]+"))) {
head = Expr({1, 0, 0}, Variable("_", tokens[1], DOUBLE));
} else if (tokens[1][0] == '\"' && tokens[1][tokens[1].length()-1] == '\"') {
ErrHandler().CallErr("operator + in unary use cannot be used with string constant");
} else {
ErrHandler().CallErr("operator + in unary use cannot be used with lvalue");
}
return head;
}
head = Expr({0, 0, 0}, Variable("_", "+", OPERATOR));
head.SetChildren({
ParseExpr(std::vector<String>(tokens.begin(), tokens.begin()+idx)),
Expand All @@ -188,7 +207,22 @@ Expr ParseExpr(std::vector<String> tokens) {
return head;
}
if (token == "-") {
if (idx == 0 || idx == tokens.size()-1) ErrHandler().CallErr("operator - cannot be unary");
if (idx == tokens.size()-1) ErrHandler().CallErr("operator - cannot be unary");
if (std::find(operators.begin(), operators.end(), tokens[idx - 1]) != operators.end())
continue;
if (idx == 0) {
if (tokens.size() != 2) ErrHandler().CallErr("invalid unary operation form");
if (std::regex_match(tokens[1], std::regex("[0-9]+"))) {
head = Expr({1, 0, 0}, Variable("_", "-" + tokens[1], INT));
} else if (std::regex_match(tokens[1], std::regex("[0-9]+.[0-9]+"))) {
head = Expr({1, 0, 0}, Variable("_", "-" + tokens[1], DOUBLE));
} else if (tokens[1][0] == '\"' && tokens[1][tokens[1].length()-1] == '\"') {
ErrHandler().CallErr("operator - in unary use cannot be used with string constant");
} else {
ErrHandler().CallErr("operator - in unary use cannot be used with lvalue");
}
return head;
}
head = Expr({0, 0, 0}, Variable("_", "-", OPERATOR));
head.SetChildren({
ParseExpr(std::vector<String>(tokens.begin(), tokens.begin()+idx)),
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class AST {
if (storage.find(v_identifier.GetValue()) == storage.end())
ErrHandler().CallErr("Variable " + v_identifier.GetValue() + " hasn't defined yet.");

storage[v_identifier.GetValue()] = expression[0].Execute(storage);
storage[v_identifier.GetValue()].Substitute(expression[0].Execute(storage).GetValue());
break;
}

Expand Down
5 changes: 3 additions & 2 deletions src/type/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class Variable {
TYPE _t;

// constructor
Variable(std::string varname = "_", std::string val = "", TYPE t = INT, bool con = 0) {
Variable(std::string varname = "_", std::string val = "", TYPE t = OPERATOR, bool con = 0) {
if (varname == "") ErrHandler().CallErr("Name of variable should not be blank. How about using '_'?");

_t = t;
token = varname;
constant = con;
Substitute(val);
if (Substitute(val) == ERROR) ErrHandler().CallErr("Type of value does not match with declaration");
}

// operation
Expand Down Expand Up @@ -282,6 +282,7 @@ class Variable {
};

inline Err Variable::Substitute(std::string newval) {
if (newval == "") return OK;
try {
switch (_t) {
case INT:
Expand Down

0 comments on commit 259b873

Please sign in to comment.