Skip to content

Commit

Permalink
Support unary for loop logic, Go 1.18rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Mar 14, 2022
1 parent 749fca3 commit a54afdc
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18.0-beta2
go-version: 1.18.0-rc1
stable: false

- name: Check out code into the Go module directory
Expand All @@ -47,7 +47,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18.0-beta2
go-version: 1.18.0-rc1
stable: false

- name: Check out code into the Go module directory
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18.0-beta2
go-version: 1.18.0-rc1
stable: false

- name: Check out code into the Go module directory
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18.0-beta2
go-version: 1.18.0-rc1
stable: false

- name: Check out code into the Go module directory
Expand Down
29 changes: 29 additions & 0 deletions tests/transpiler/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,35 @@ jump 6 always
print _main_i
op add _main_i _main_i 1
jump 3 lessThan _main_i 10`,
},
{
name: "ForLoop2",
input: TestMain(`x := false
for !x {
x = true
}`, false, false),
output: `set _main_x false
jump 3 notEqual _main_x true
jump 5 always
set _main_x true
jump 3 notEqual _main_x true`,
},
{
name: "ForLoop3",
input: TestMain(`s := m.B("switch1")
a := 1
b := 2
for s.IsEnabled() && a < b {
print(1)
}`, true, false),
output: `set _main_s switch1
set _main_a 1
set _main_b 2
sensor _main_0 _main_s @enabled
jump 6 equal _main_0 true
jump 8 always
print 1
jump 3 always`,
},
{
name: "Reassignment",
Expand Down
99 changes: 78 additions & 21 deletions transpiler/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,21 @@ func forStmtToMLOG(ctx context.Context, statement *ast.ForStmt) ([]MLOGStatement
var loopStartJump *MLOGJump
var intoLoopJump *MLOGJump

var loopStartOverride *MLOGStatement

if statement.Cond != nil {
loopStartJump = &MLOGJump{
MLOG: MLOG{
Comment: "Jump to start of loop",
},
}

intoLoopJump = &MLOGJump{
MLOG: MLOG{
Comment: "Jump into the loop",
},
}

if binaryExpr, ok := statement.Cond.(*ast.BinaryExpr); ok {
if translatedOp, ok := jumpOperators[binaryExpr.Op]; ok {
leftSide, leftExprInstructions, err := exprToResolvable(ctx, binaryExpr.X)
Expand All @@ -342,32 +356,70 @@ func forStmtToMLOG(ctx context.Context, statement *ast.ForStmt) ([]MLOGStatement
return nil, Err(ctx, "unknown error")
}

loopStartJump = &MLOGJump{
MLOG: MLOG{
Comment: "Jump to start of loop",
},
Condition: []Resolvable{
&Value{Value: translatedOp},
leftSide[0],
rightSide[0],
},
loopStartJump.Condition = []Resolvable{
&Value{Value: translatedOp},
leftSide[0],
rightSide[0],
}

intoLoopJump = &MLOGJump{
MLOG: MLOG{
Comment: "Jump into the loop",
},
Condition: []Resolvable{
&Value{Value: translatedOp},
leftSide[0],
rightSide[0],
},
intoLoopJump.Condition = []Resolvable{
&Value{Value: translatedOp},
leftSide[0],
rightSide[0],
}
} else {
return nil, Err(ctx, fmt.Sprintf("jump statement cannot use this operation: %T", binaryExpr.Op))
expr, exprInstructions, err := exprToResolvable(ctx, binaryExpr.X)
if err != nil {
return nil, err
}

results = append(results, exprInstructions...)

if len(expr) != 1 {
return nil, Err(ctx, "unknown error")
}

loopStartJump.Condition = []Resolvable{
&Value{Value: "always"},
}

intoLoopJump.Condition = []Resolvable{
&Value{Value: jumpOperators[token.EQL]},
expr[0],
&Value{Value: "true"},
}

loopStartOverride = &exprInstructions[0]
}
} else if unaryExpr, ok := statement.Cond.(*ast.UnaryExpr); ok {
if unaryExpr.Op != token.NOT {
return nil, Err(ctx, fmt.Sprintf("loop unary expresion cannot use this operation: %T", binaryExpr.Op))
}

expr, exprInstructions, err := exprToResolvable(ctx, unaryExpr.X)
if err != nil {
return nil, err
}

results = append(results, exprInstructions...)

if len(expr) != 1 {
return nil, Err(ctx, "unknown error")
}

loopStartJump.Condition = []Resolvable{
&Value{Value: jumpOperators[token.NEQ]},
expr[0],
&Value{Value: "true"},
}

intoLoopJump.Condition = []Resolvable{
&Value{Value: jumpOperators[token.NEQ]},
expr[0],
&Value{Value: "true"},
}
} else {
return nil, Err(ctx, "for loop can only have binary conditional expressions")
return nil, Err(ctx, "for loop can only have unary or binary conditional expressions")
}
} else {
loopStartJump = &MLOGJump{
Expand Down Expand Up @@ -425,7 +477,12 @@ func forStmtToMLOG(ctx context.Context, statement *ast.ForStmt) ([]MLOGStatement
blockCtxStruct.Extra = append(blockCtxStruct.Extra, instructions...)
}

loopStartJump.JumpTarget = bodyMLOG[0]
if loopStartOverride != nil {
loopStartJump.JumpTarget = *loopStartOverride
} else {
loopStartJump.JumpTarget = bodyMLOG[0]
}

results = append(results, loopStartJump)
blockCtxStruct.Extra = append(blockCtxStruct.Extra, loopStartJump)

Expand Down

0 comments on commit a54afdc

Please sign in to comment.