Skip to content

Commit a54afdc

Browse files
committed
Support unary for loop logic, Go 1.18rc1
1 parent 749fca3 commit a54afdc

File tree

4 files changed

+111
-25
lines changed

4 files changed

+111
-25
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- name: Set up Go
3030
uses: actions/setup-go@v2
3131
with:
32-
go-version: 1.18.0-beta2
32+
go-version: 1.18.0-rc1
3333
stable: false
3434

3535
- name: Check out code into the Go module directory
@@ -47,7 +47,7 @@ jobs:
4747
- name: Set up Go
4848
uses: actions/setup-go@v2
4949
with:
50-
go-version: 1.18.0-beta2
50+
go-version: 1.18.0-rc1
5151
stable: false
5252

5353
- name: Check out code into the Go module directory

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Set up Go
1414
uses: actions/setup-go@v2
1515
with:
16-
go-version: 1.18.0-beta2
16+
go-version: 1.18.0-rc1
1717
stable: false
1818

1919
- name: Check out code into the Go module directory
@@ -47,7 +47,7 @@ jobs:
4747
- name: Set up Go
4848
uses: actions/setup-go@v2
4949
with:
50-
go-version: 1.18.0-beta2
50+
go-version: 1.18.0-rc1
5151
stable: false
5252

5353
- name: Check out code into the Go module directory

tests/transpiler/statement_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ jump 6 always
3737
print _main_i
3838
op add _main_i _main_i 1
3939
jump 3 lessThan _main_i 10`,
40+
},
41+
{
42+
name: "ForLoop2",
43+
input: TestMain(`x := false
44+
for !x {
45+
x = true
46+
}`, false, false),
47+
output: `set _main_x false
48+
jump 3 notEqual _main_x true
49+
jump 5 always
50+
set _main_x true
51+
jump 3 notEqual _main_x true`,
52+
},
53+
{
54+
name: "ForLoop3",
55+
input: TestMain(`s := m.B("switch1")
56+
a := 1
57+
b := 2
58+
for s.IsEnabled() && a < b {
59+
print(1)
60+
}`, true, false),
61+
output: `set _main_s switch1
62+
set _main_a 1
63+
set _main_b 2
64+
sensor _main_0 _main_s @enabled
65+
jump 6 equal _main_0 true
66+
jump 8 always
67+
print 1
68+
jump 3 always`,
4069
},
4170
{
4271
name: "Reassignment",

transpiler/statement.go

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,21 @@ func forStmtToMLOG(ctx context.Context, statement *ast.ForStmt) ([]MLOGStatement
319319
var loopStartJump *MLOGJump
320320
var intoLoopJump *MLOGJump
321321

322+
var loopStartOverride *MLOGStatement
323+
322324
if statement.Cond != nil {
325+
loopStartJump = &MLOGJump{
326+
MLOG: MLOG{
327+
Comment: "Jump to start of loop",
328+
},
329+
}
330+
331+
intoLoopJump = &MLOGJump{
332+
MLOG: MLOG{
333+
Comment: "Jump into the loop",
334+
},
335+
}
336+
323337
if binaryExpr, ok := statement.Cond.(*ast.BinaryExpr); ok {
324338
if translatedOp, ok := jumpOperators[binaryExpr.Op]; ok {
325339
leftSide, leftExprInstructions, err := exprToResolvable(ctx, binaryExpr.X)
@@ -342,32 +356,70 @@ func forStmtToMLOG(ctx context.Context, statement *ast.ForStmt) ([]MLOGStatement
342356
return nil, Err(ctx, "unknown error")
343357
}
344358

345-
loopStartJump = &MLOGJump{
346-
MLOG: MLOG{
347-
Comment: "Jump to start of loop",
348-
},
349-
Condition: []Resolvable{
350-
&Value{Value: translatedOp},
351-
leftSide[0],
352-
rightSide[0],
353-
},
359+
loopStartJump.Condition = []Resolvable{
360+
&Value{Value: translatedOp},
361+
leftSide[0],
362+
rightSide[0],
354363
}
355364

356-
intoLoopJump = &MLOGJump{
357-
MLOG: MLOG{
358-
Comment: "Jump into the loop",
359-
},
360-
Condition: []Resolvable{
361-
&Value{Value: translatedOp},
362-
leftSide[0],
363-
rightSide[0],
364-
},
365+
intoLoopJump.Condition = []Resolvable{
366+
&Value{Value: translatedOp},
367+
leftSide[0],
368+
rightSide[0],
365369
}
366370
} else {
367-
return nil, Err(ctx, fmt.Sprintf("jump statement cannot use this operation: %T", binaryExpr.Op))
371+
expr, exprInstructions, err := exprToResolvable(ctx, binaryExpr.X)
372+
if err != nil {
373+
return nil, err
374+
}
375+
376+
results = append(results, exprInstructions...)
377+
378+
if len(expr) != 1 {
379+
return nil, Err(ctx, "unknown error")
380+
}
381+
382+
loopStartJump.Condition = []Resolvable{
383+
&Value{Value: "always"},
384+
}
385+
386+
intoLoopJump.Condition = []Resolvable{
387+
&Value{Value: jumpOperators[token.EQL]},
388+
expr[0],
389+
&Value{Value: "true"},
390+
}
391+
392+
loopStartOverride = &exprInstructions[0]
393+
}
394+
} else if unaryExpr, ok := statement.Cond.(*ast.UnaryExpr); ok {
395+
if unaryExpr.Op != token.NOT {
396+
return nil, Err(ctx, fmt.Sprintf("loop unary expresion cannot use this operation: %T", binaryExpr.Op))
397+
}
398+
399+
expr, exprInstructions, err := exprToResolvable(ctx, unaryExpr.X)
400+
if err != nil {
401+
return nil, err
402+
}
403+
404+
results = append(results, exprInstructions...)
405+
406+
if len(expr) != 1 {
407+
return nil, Err(ctx, "unknown error")
408+
}
409+
410+
loopStartJump.Condition = []Resolvable{
411+
&Value{Value: jumpOperators[token.NEQ]},
412+
expr[0],
413+
&Value{Value: "true"},
414+
}
415+
416+
intoLoopJump.Condition = []Resolvable{
417+
&Value{Value: jumpOperators[token.NEQ]},
418+
expr[0],
419+
&Value{Value: "true"},
368420
}
369421
} else {
370-
return nil, Err(ctx, "for loop can only have binary conditional expressions")
422+
return nil, Err(ctx, "for loop can only have unary or binary conditional expressions")
371423
}
372424
} else {
373425
loopStartJump = &MLOGJump{
@@ -425,7 +477,12 @@ func forStmtToMLOG(ctx context.Context, statement *ast.ForStmt) ([]MLOGStatement
425477
blockCtxStruct.Extra = append(blockCtxStruct.Extra, instructions...)
426478
}
427479

428-
loopStartJump.JumpTarget = bodyMLOG[0]
480+
if loopStartOverride != nil {
481+
loopStartJump.JumpTarget = *loopStartOverride
482+
} else {
483+
loopStartJump.JumpTarget = bodyMLOG[0]
484+
}
485+
429486
results = append(results, loopStartJump)
430487
blockCtxStruct.Extra = append(blockCtxStruct.Extra, loopStartJump)
431488

0 commit comments

Comments
 (0)