Skip to content

Commit

Permalink
feat(type_checker,parser,intellij,vscode): foreach can now skip data …
Browse files Browse the repository at this point in the history
…type
  • Loading branch information
JaDogg committed Jul 1, 2023
1 parent 1a5fd71 commit a54a97c
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 29 deletions.
3 changes: 3 additions & 0 deletions compiler/scripts/update_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
# For loop
# for x: int in expr:
# println(x)
# ## or
# for x in expr:
# println(x)
("foreach", (("token*", "for_keyword"), ("token*", "name"), ("ykdatatype*", "data_type"),
("token*", "in_keyword"), ("expr*", "expression"), ("stmt*", "for_body"),)),
# For loop - endless loop
Expand Down
7 changes: 5 additions & 2 deletions compiler/src/ast/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,11 @@ stmt *parser::for_statement() {
token_type::
NAME)) {// foreach_stmt -> FOR name: datatype in array: block_stmt
auto name = consume(token_type::NAME, "Name must be present");
consume(token_type::COLON, "Colon must be present");
auto dt = parse_datatype();
ykdatatype* dt = nullptr;
if (check(token_type::COLON)) {
consume(token_type::COLON, "Colon must be present");
dt = parse_datatype();
}
auto in_k =
consume(token_type::KEYWORD_IN,
"in must follow the data type declaration in for each loop.");
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/compiler/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,10 @@ void type_checker::visit_foreach_stmt(foreach_stmt *obj) {
error(obj->for_keyword_,
"Cannot use foreach iteration for SMEntry and MEntry.");
}
// Infer data type of foreach
if (obj->data_type_ == nullptr) {
obj->data_type_ = exp.datatype_->args_[0];
}
auto lhs = exp.datatype_->args_[0];
auto rhs = obj->data_type_;
if ((*lhs != *rhs)) {
Expand Down
4 changes: 2 additions & 2 deletions compiler/test_data/compiler_tests/eachelem_for.yaka
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
def main() -> int:
e1: Array[int] = array("int", 1, 2, 3)
e2: Array[int] = array("int", 4, 5, 6, 7)
e2 = array("int", 4, 5, 6, 7)
for i: int in e1:
for j: int in e2:
for j in e2:
print(i)
print(" - ")
println(j)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ if_statement ::= I KW_IF S? exp S? OPERATOR_COLON S? def_block
elif_statement ::= I KW_ELIF S? exp S? OPERATOR_COLON S? def_block
else_statement ::= I KW_ELSE S? OPERATOR_COLON S? def_block
while_statement ::= I KW_WHILE S? exp S? OPERATOR_COLON S? def_block
foreach_statement ::= I KW_FOR S? IDENTIFIER S? OPERATOR_COLON S? data_type S? KW_IN S? exp S? OPERATOR_COLON S? def_block
foreach_statement ::= I KW_FOR S? IDENTIFIER (S? OPERATOR_COLON S? data_type)? S? KW_IN S? exp S? OPERATOR_COLON S? def_block
forendless_statement ::= I KW_FOR S? OPERATOR_COLON S? def_block

// Simple statements
Expand Down
2 changes: 1 addition & 1 deletion editor/vscode/language-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
],
"onEnterRules": [
{
"beforeText": "^\\s*(?:def|class|for|if|elif|else|while).*?:\\s*$",
"beforeText": "^\\s*(?:def|class|struct|for|if|elif|else|while).*?:\\s*$",
"action": { "indent": "indent" }
}
]
Expand Down
2 changes: 1 addition & 1 deletion editor/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/YakshaLang/Yaksha.git"
},
"version": "0.0.2",
"version": "0.0.3",
"engines": {
"vscode": "^1.64.0"
},
Expand Down

0 comments on commit a54a97c

Please sign in to comment.