Skip to content

Commit

Permalink
starting on syntax sugar for constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-balades committed Nov 14, 2022
1 parent 0ebc3fa commit 5d3d7a0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,6 @@
"source_location": "cpp",
"charconv": "cpp"
},
"C_Cpp.errorSquiggles": "Disabled"
"C_Cpp.errorSquiggles": "Disabled",
"todo-tree.tree.scanMode": "workspace"
}
37 changes: 27 additions & 10 deletions src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ namespace snowball {
_context.current_class = cls;
while (true) {
next_token();

#define IS_CONSTRUCTOR(tk) tk.type == TokenType::IDENTIFIER && tk.value == cls->name
#define SET_TOKEN() _current_token = peek(0);

switch (_current_token.type)
{
case TokenType::BRACKET_RCURLY: {
Expand All @@ -403,28 +407,32 @@ namespace snowball {
}

case TokenType::KWORD_STATIC: {
if (peek(0, true).type != TokenType::KWORD_FUNC && peek(0, true).type != TokenType::KWORD_VAR) {
if (peek().type != TokenType::KWORD_FUNC && peek().type != TokenType::KWORD_VAR) {
PARSER_ERROR(Error::SYNTAX_ERROR, "expected keyword \"func\" or \"var\" after static");
}
} break;

case TokenType::KWORD_VIRTUAL: {
// PARSER_ERROR(Error::TODO, "Virtual not yet supported")
if (peek(0, true).type == TokenType::KWORD_STATIC) {
if (peek().type == TokenType::KWORD_STATIC) {
SET_TOKEN()
PARSER_ERROR(Error::ARGUMENT_ERROR, "Virtual methods can't be static!");
} else if (peek(0, true).type != TokenType::KWORD_FUNC && peek(0, true).type != TokenType::KWORD_VAR) {
} else if (peek().type != TokenType::KWORD_FUNC && peek().type != TokenType::KWORD_VAR) {
SET_TOKEN()
PARSER_ERROR(Error::SYNTAX_ERROR, "expected keyword \"func\" or \"var\" after virtual declaration");
}
} break;

case TokenType::KWORD_PUBLIC:
case TokenType::KWORD_PRIVATE: {
if (
peek(0, true).type != TokenType::KWORD_FUNC
&& peek(0, true).type != TokenType::KWORD_VAR
&& peek(0, true).type != TokenType::KWORD_VIRTUAL
&& peek(0, true).type != TokenType::KWORD_STATIC
&& peek(0, true).type != TokenType::KWORD_OPERATOR) {
peek().type != TokenType::KWORD_FUNC
&& peek().type != TokenType::KWORD_VAR
&& peek().type != TokenType::KWORD_VIRTUAL
&& peek().type != TokenType::KWORD_STATIC
&& peek().type != TokenType::KWORD_OPERATOR
&& (!(IS_CONSTRUCTOR(peek())))) {
SET_TOKEN()
PARSER_ERROR(Error::SYNTAX_ERROR, "expected keyword \"func\", \"virt\", \"var\", \"operator\" or \"static\" after pub/priv declaration");
}
break;
Expand All @@ -445,9 +453,20 @@ namespace snowball {
cls->functions.push_back(func);
} break;

case TokenType::IDENTIFIER: {
if (IS_CONSTRUCTOR(_current_token)) {
// TODO: parse constructor
break;
}

// fall through
}

default:
PARSER_ERROR(Error::SYNTAX_ERROR, Logger::format("'%s' is not allowed inside class blocks", _current_token.to_string().c_str()))
}

#undef IS_CONSTRUCTOR
}

// TODO: this does not work if class has 2 duplicate variables
Expand Down Expand Up @@ -519,8 +538,6 @@ namespace snowball {
OP_CASE(OP_BIT_XOR, BIT_XOR);
OP_CASE(OP_BIT_XOR_EQ, BIT_XOR_EQ);

OP_CASE(KWORD_NEW, CONSTRUCTOR);

case TokenType::BRACKET_LPARENT: {
if (peek().type == TokenType::BRACKET_RPARENT) {
next_token();
Expand Down
4 changes: 2 additions & 2 deletions tests/main.sn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import System

class Parent {
pub var hello: String = ""
pub operator new() {}
Padrent() {}

pub virt fn hello() {
System.println("parent")
Expand All @@ -14,7 +14,7 @@ class Parent {
class Child : Parent {
// pub var hello: String = "hey"

pub operator new() {}
pub Child() {}

pub virt fn hello() {
System.println(self.hello)
Expand Down

0 comments on commit 5d3d7a0

Please sign in to comment.