Skip to content

Commit

Permalink
forExpander: consume and emit inner for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
bobertlo committed Dec 4, 2024
1 parent 758d2c7 commit 480f02b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
29 changes: 23 additions & 6 deletions forexpand.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type forExpander struct {
forCount int
forIndex int
forContent []token
forDepth int

symbols map[string][]token

Expand Down Expand Up @@ -223,6 +224,7 @@ func forFor(f *forExpander) forStateFn {

if len(f.labelBuf) > 0 {
f.forCountLabel = f.labelBuf[len(f.labelBuf)-1]

if len(f.labelBuf) > 1 {
f.forLineLabels = f.labelBuf[:len(f.labelBuf)-1]
} else {
Expand All @@ -246,6 +248,7 @@ func forFor(f *forExpander) forStateFn {
func forInnerLine(f *forExpander) forStateFn {
switch f.nextToken.typ {
case tokText:
f.labelBuf = make([]string, 0)
return forInnerLabels
default:
// emitconsume line into for buffer
Expand All @@ -257,10 +260,22 @@ func forInnerLine(f *forExpander) forStateFn {
func forInnerLabels(f *forExpander) forStateFn {
switch f.nextToken.typ {
case tokText:
if f.nextToken.IsPseudoOp() && strings.ToLower(f.nextToken.val) == "rof" {
// just call to emit the buffer

return forRof
if f.nextToken.IsPseudoOp() {
opLower := strings.ToLower(f.nextToken.val)
if opLower == "for" {
fmt.Println("inner:", f.labelBuf, "for")
f.forDepth += 1
return forInnerEmitLabels
} else if opLower == "rof" {
if f.forDepth > 0 {
f.forDepth -= 1
return forInnerEmitConsumeLine
} else {
return forRof
}
} else {
return forInnerEmitLabels
}
} else if f.nextToken.IsOp() {
// write labels and op into emit buffer then emitcomsume line
return forInnerEmitLabels
Expand All @@ -270,14 +285,14 @@ func forInnerLabels(f *forExpander) forStateFn {
return forInnerLabels
}
default:
// not expecting legal input here, but we will let the parser deal with it
return forInnerEmitLabels
// emit labels and emitconsume line into for buffer
}
}

func forInnerEmitLabels(f *forExpander) forStateFn {
for _, label := range f.labelBuf {
f.tokens <- token{tokText, label}
f.forContent = append(f.forContent, token{tokText, label})
}
return forInnerEmitConsumeLine
}
Expand Down Expand Up @@ -310,6 +325,8 @@ func forRof(f *forExpander) forStateFn {
}
f.next()

fmt.Println(f.forContent)

for i := 1; i <= f.forCount; i++ {
for _, tok := range f.forContent {
if tok.typ == tokText {
Expand Down
28 changes: 28 additions & 0 deletions forexpand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ func runForExpanderTests(t *testing.T, cases []forTestCase) {

func TestForExpander(t *testing.T) {
tests := []forTestCase{
{
input: "i for 2\nj for 2\ndat i, j\nrof\nrof\n",
output: []token{
{tokText, "j"},
{tokText, "for"},
{tokNumber, "2"},
{tokNewline, ""},
{tokText, "dat"},
{tokNumber, "1"},
{tokComma, ","},
{tokText, "j"},
{tokNewline, ""},
{tokText, "rof"},
{tokNewline, ""},
{tokText, "j"},
{tokText, "for"},
{tokNumber, "2"},
{tokNewline, ""},
{tokText, "dat"},
{tokNumber, "2"},
{tokComma, ","},
{tokText, "j"},
{tokNewline, ""},
{tokText, "rof"},
{tokNewline, ""},
{tokEOF, ""},
},
},
{
input: "i for 2\ndat 0, i\nrof\n",
output: []token{
Expand Down

0 comments on commit 480f02b

Please sign in to comment.