Skip to content

Commit

Permalink
usage of 'xxxRange' and 'in'
Browse files Browse the repository at this point in the history
  • Loading branch information
objeck committed Feb 18, 2024
1 parent 6e7b182 commit d754bdb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
35 changes: 25 additions & 10 deletions core/compiler/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5068,15 +5068,12 @@ For* Parser::ParseEach(bool reverse, int depth)
NextToken();

// add count entry
Assignment* bind_assign = nullptr;
std::wstring bound_ident;
if(bind_var) {
bound_ident = count_ident;
count_ident = L'#' + count_ident + L"_index";
}

// add bind variable entry
Assignment* bind_assign = nullptr;
if(bind_var) {

const std::wstring bind_scope_name = GetScopeName(bound_ident);
Type* bind_left_type = TypeFactory::Instance()->MakeType(VAR_TYPE);
SymbolEntry* bind_entry = TreeFactory::Instance()->MakeSymbolEntry(file_name, line_num, line_pos, bind_scope_name,
Expand All @@ -5098,10 +5095,28 @@ For* Parser::ParseEach(bool reverse, int depth)
if(Match(TOKEN_IDENT)) {
const std::wstring ident_type = scanner->GetToken()->GetIdentifier();
if(ident_type == L"CharRange" || ident_type == L"System.CharRange") {
count_type = TypeFactory::Instance()->MakeType(CHAR_TYPE);
if(!bind_var) {
count_type = TypeFactory::Instance()->MakeType(CHAR_TYPE);
}
else {
ProcessError(L"Range class '" + ident_type + L"' cannot be bound to index variable '" + bound_ident + L"', try using the ':' operator");
}
}
else if((ident_type == L"IntRange" || ident_type == L"System.IntRange")) {
if(!bind_var) {
count_type = TypeFactory::Instance()->MakeType(INT_TYPE);
}
else {
ProcessError(L"Range class '" + ident_type + L"' cannot be bound to index variable '" + bound_ident + L"', try using the ':' operator");
}
}
else if(ident_type == L"FloatRange" || ident_type == L"System.FloatRange") {
count_type = TypeFactory::Instance()->MakeType(FLOAT_TYPE);
if(!bind_var) {
count_type = TypeFactory::Instance()->MakeType(FLOAT_TYPE);
}
else {
ProcessError(L"Range class '" + ident_type + L"' cannot be bound to index variable '" + bound_ident + L"', try using the ':' operator");
}
}
}

Expand Down Expand Up @@ -5220,7 +5235,7 @@ For* Parser::ParseEach(bool reverse, int depth)
//
left_pre_count = ParseExpression(depth + 1);

if(left_pre_count->GetExpressionType() == VAR_EXPR) {
if(left_pre_count && left_pre_count->GetExpressionType() == VAR_EXPR) {
Variable* variable = static_cast<Variable*>(left_pre_count);
if(!variable->GetIndices()) {
// set method call to 'x->Size()'
Expand All @@ -5229,7 +5244,7 @@ For* Parser::ParseEach(bool reverse, int depth)
variable->GetName(), L"Size", TreeFactory::Instance()->MakeExpressionList());
}
}
else if(left_pre_count->GetExpressionType() == METHOD_CALL_EXPR) {
else if(left_pre_count && left_pre_count->GetExpressionType() == METHOD_CALL_EXPR) {
// add count entry
const std::wstring count_scope_name = GetScopeName(L'#' + count_ident + L"_range");
Type* count_type = TypeFactory::Instance()->MakeType(CLASS_TYPE);
Expand All @@ -5242,7 +5257,7 @@ For* Parser::ParseEach(bool reverse, int depth)
ProcessError(L"Variable already defined in this scope: '" + count_ident + L"'");
}
}
else if(left_pre_count->GetExpressionType() != METHOD_CALL_EXPR) {
else if(left_pre_count && left_pre_count->GetExpressionType() != METHOD_CALL_EXPR) {
ProcessError(L"Expected variable or literal expression", TOKEN_SEMI_COLON);
}
}
Expand Down
Binary file modified docs/api.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions programs/tests/prgm277.obs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ class Test {
(0.1 + 0.2 = 0.3)->PrintLine();

x := 13.3;

#~
for(i := 5; i < x; i += 3;) {
i->PrintLine();
}

each(i : IntRange->New(5., x, 3)) {
~#
each(i : IntRange->New(5)) {
i->PrintLine();
}

Expand Down
5 changes: 2 additions & 3 deletions programs/tests/prgm283.obs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class Test {
function : Main(args : String[]) ~ Nil {
values := [1,2,3,4,5];
each(value in values) {
value->PrintLine()
each(value in IntRange->New(1,4)) {
value->Print()
}
}
}

0 comments on commit d754bdb

Please sign in to comment.