Skip to content

Commit

Permalink
Add structure for 'from import' statement
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmalonenz committed Apr 16, 2024
1 parent 808867c commit ac8ebaa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions vmlib/compiler/src/statements.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ void importStatement(Parser *parser)
defineVariable(parser, global);
}

void fromImportStatement(Parser* parser)
{
expression(parser);
emitByte(parser, OP_IMPORT);
// Imports are a function that return NIL, so ditch the nil from the stack
emitByte(parser, OP_POP);
}

void nextStatement(Parser *parser)
{
if (parser->currentLoop == NULL) {
Expand Down Expand Up @@ -415,6 +423,10 @@ void statement(Parser *parser)
{
importStatement(parser);
}
else if (match(parser, TOKEN_FROM))
{
fromImportStatement(parser);
}
else if (match(parser, TOKEN_NEXT))
{
nextStatement(parser);
Expand Down
2 changes: 1 addition & 1 deletion vmlib/lexer/inc/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ typedef enum {

// Keywords.
TOKEN_AS, TOKEN_BREAK, TOKEN_CLASS, TOKEN_ELSE, TOKEN_ENUM, TOKEN_FALSE,
TOKEN_FOR, TOKEN_FOREACH, TOKEN_FUN, TOKEN_IF, TOKEN_IMPORT, TOKEN_IN,
TOKEN_FOR, TOKEN_FOREACH, TOKEN_FROM, TOKEN_FUN, TOKEN_IF, TOKEN_IMPORT, TOKEN_IN,
TOKEN_IS, TOKEN_NEXT, TOKEN_NIL, TOKEN_OPERATOR, TOKEN_RETHROW,
TOKEN_RETURN, TOKEN_SELF, TOKEN_SUPER, TOKEN_TRUE, TOKEN_VAR, TOKEN_WHILE,
TOKEN_TRY, TOKEN_CATCH, TOKEN_THROW, TOKEN_FINAL, TOKEN_FINALLY,
Expand Down
4 changes: 4 additions & 0 deletions vmlib/lexer/src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ static TokenType_t identifierType(Scanner *scanner)
return checkKeyword(scanner, 2, 5, "reach", TOKEN_FOREACH);
return checkKeyword(scanner, 2, 1, "r", TOKEN_FOR);
}
case 'r':
{
return checkKeyword(scanner, 2, 2, "om", TOKEN_FROM);
}
case 'u':
return checkKeyword(scanner, 2, 6, "nction", TOKEN_FUN);
}
Expand Down
1 change: 1 addition & 0 deletions vmlib/lexer/test/test_scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ char *token_strings[NUM_TOKENS] =
[TOKEN_FALSE] = "false",
[TOKEN_FOR] = "for",
[TOKEN_FOREACH] = "foreach",
[TOKEN_FROM] = "from",
[TOKEN_FUN] = "function",
[TOKEN_IF] = "if",
[TOKEN_IMPORT] = "import",
Expand Down

0 comments on commit ac8ebaa

Please sign in to comment.