Skip to content

Commit

Permalink
fixed specification parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
maniospas committed Oct 8, 2024
1 parent 9cf9a14 commit 2abecd5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
Binary file modified blombly.exe
Binary file not shown.
18 changes: 8 additions & 10 deletions main.bb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
routes = server(5000);
routes["/"] = {
return "<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>"
final hello = { // code block that can be called or inlined elsewhere
#spec author = "maniospas"; // any spec field can be set
#spec version = "v1.0.0"; // can set field to objects too
print("Hello "+name+"!");
}
while(true) {} // wait indefinitely
print(hello.version);
name = read("What's your name?");
hello(name=name);
30 changes: 19 additions & 11 deletions main.bbvm
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
BUILTIN _bb0 I5000
server routes _bb0
BEGIN _bb2
BUILTIN _bb3 "<!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html>"
return _bbresult0 _bb3
BEGIN _bb0
BUILTIN _bb2 "!"
BUILTIN _bb4 "Hello "
add _bb3 _bb4 name
add _bb1 _bb3 _bb2
print # _bb1
END
BUILTIN _bb4 "/"
put # routes _bb4 _bb2
BUILTIN _bb5 Btrue
BEGIN _bb6
BUILTIN _bb5 Btrue
IS hello _bb0
final # hello
BUILTIN _bb5 "v1.0.0"
setfinal # hello version _bb5
BUILTIN _bb6 "maniospas"
setfinal # hello author _bb6
get _bb7 hello version
print # _bb7
BUILTIN _bb8 "What's your name?"
read name _bb8
BEGIN _bb11
IS name name
END
while # _bb5 _bb6
call _bb10 _bb11 hello
31 changes: 14 additions & 17 deletions src/utils/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,14 +1015,13 @@ void macros(std::vector<Token>& tokens, const std::string& first_source) {
if (i > 1) {
bbassert(tokens[i - 1].name == "{",
"Unexpected `#spec` encountered."
"\n \033[33m!!!\033[0m `#spec` declarations can only reside"
"\n at the beginning of their enclosing code block.\n"
"\n \033[33m!!!\033[0m Each `#spec` declaration can only reside in named code blocks."
"\n These refer to code blocks being declared and assigned to at least one variable.\n"
+Parser::show_position(tokens, i));
bbassert(tokens[i - 2].name == "=" || tokens[i - 2].name == "as",
"Invalid `#spec` syntax."
"\n \033[33m!!!\033[0m Each `#spec` declaration can only reside"
"\n in named code blocks. These refer to code blocks being"
"\n declared and assigned to at least one variable.\n"
"\n \033[33m!!!\033[0m Each `#spec` declaration can only reside in named code blocks."
"\n These refer to code blocks being declared and assigned to at least one variable.\n"
+Parser::show_position(tokens, i));
int position = i - 1;
int depth = 0;
Expand All @@ -1041,14 +1040,13 @@ void macros(std::vector<Token>& tokens, const std::string& first_source) {
}
bbassert(depth == 0,
"Unexpected `#spec` encountered."
"\n \033[33m!!!\033[0m `#spec` declarations can only reside "
"\n in named code blocks. These refer to code blocks being"
"\n declared and assigned to at least one variable.\n"
"\n \033[33m!!!\033[0m Each `#spec` declaration can only reside in named code blocks."
"\n These refer to code blocks being declared and assigned to at least one variable.\n"
+Parser::show_position(tokens, i));
specNameEnd = position - 1;
specNameStart = specNameEnd;
depth = 0;
while (specNameStart > 0) {
while (specNameStart >= 0) {
if (tokens[specNameStart].name == "[" ||
tokens[specNameStart].name == "(" ||
tokens[specNameStart].name == "{")
Expand All @@ -1059,16 +1057,15 @@ void macros(std::vector<Token>& tokens, const std::string& first_source) {
depth -= 1;
if ((tokens[specNameStart].name == ";" ||
tokens[specNameStart].name == "final") && depth == 0) {
specNameStart += 1;
break;
}
specNameStart -= 1;
}
specNameStart += 1;
bbassert(depth >= 0,
"Unexpected `#spec` encountered."
"\n \033[33m!!!\033[0m `#spec` declarations can only reside "
"\n in named code blocks. These refer to code blocks being"
"\n declared and assigned to at least one variable.\n"
"\n \033[33m!!!\033[0m Each `#spec` declaration can only reside in named code blocks."
"\n These refer to code blocks being declared and assigned to at least one variable.\n"
+Parser::show_position(tokens, i));
}
int depth = 1;
Expand All @@ -1090,14 +1087,14 @@ void macros(std::vector<Token>& tokens, const std::string& first_source) {
break;
}
}
bbassert(position > tokens.size() - 1,
bbassert(specNameEnd < tokens.size(),
"Unexpected `#spec` encountered."
"\n \033[33m!!!\033[0m `#spec` declarations can only reside "
"\n in named code blocks. These refer to code blocks being"
"\n declared and assigned to at least one variable.\n"
"\n \033[33m!!!\033[0m Each `#spec` declaration can only reside in named code blocks."
"\n These refer to code blocks being declared and assigned to at least one variable.\n"
+Parser::show_position(tokens, i));
bbassert(depth == 0, "Imbalanced parantheses or brackets.\n" + Parser::show_position(tokens, i));


std::vector<Token> newTokens;
newTokens.emplace_back("final", tokens[i].file, tokens[i].line, false);
if (specNameEnd == MISSING || specNameEnd < specNameStart) {
Expand Down

0 comments on commit 2abecd5

Please sign in to comment.