-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
282 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package compiler | ||
|
||
import ( | ||
"github.com/llir/llvm/ir" | ||
"github.com/llir/llvm/ir/constant" | ||
|
||
"github.com/cell-labs/cell-script/compiler/compiler/internal" | ||
"github.com/cell-labs/cell-script/compiler/compiler/name" | ||
"github.com/cell-labs/cell-script/compiler/parser" | ||
) | ||
|
||
func (c *Compiler) compileSwitchNode(v *parser.SwitchNode) { | ||
switchItem := c.compileValue(v.Item) | ||
|
||
var cases []*ir.Case | ||
caseBlocks := make([]*ir.Block, len(v.Cases)) | ||
|
||
afterSwitch := c.contextBlock.Parent.NewBlock(name.Block() + "after-switch") | ||
|
||
// build default case | ||
defaultCase := c.contextBlock.Parent.NewBlock(name.Block() + "switch-default") | ||
if v.DefaultBody != nil { | ||
preDefaultBlock := c.contextBlock | ||
c.contextBlock = defaultCase | ||
c.compile(v.DefaultBody) | ||
c.contextBlock = preDefaultBlock | ||
} | ||
defaultCase.NewBr(afterSwitch) | ||
|
||
// Parse all cases | ||
for caseIndex, parseCase := range v.Cases { | ||
preCaseBlock := c.contextBlock | ||
caseBlock := c.contextBlock.Parent.NewBlock(name.Block() + "case") | ||
c.contextBlock = caseBlock | ||
c.compile(parseCase.Body) | ||
c.contextBlock = preCaseBlock | ||
|
||
caseBlocks[caseIndex] = caseBlock | ||
|
||
for _, cond := range parseCase.Conditions { | ||
item := c.compileValue(cond) | ||
cases = append(cases, ir.NewCase(item.Value.(constant.Constant), caseBlock)) | ||
} | ||
} | ||
|
||
for caseIndex, parseCase := range v.Cases { | ||
if parseCase.Fallthrough { | ||
// Jump to the next case body | ||
caseBlocks[caseIndex].Term = ir.NewBr(caseBlocks[caseIndex+1]) | ||
} else { | ||
// Jump to after switch | ||
caseBlocks[caseIndex].Term = ir.NewBr(afterSwitch) | ||
} | ||
} | ||
|
||
val := internal.LoadIfVariable(c.contextBlock, switchItem) | ||
c.contextBlock.Term = c.contextBlock.NewSwitch(val, defaultCase, cases...) | ||
|
||
c.contextBlock = afterSwitch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,30 @@ | ||
package lexer | ||
|
||
var keywords = map[string]struct{}{ | ||
"if": {}, | ||
"else": {}, | ||
"func": {}, | ||
"extern": {}, | ||
"return": {}, | ||
"type": {}, | ||
"table": {}, | ||
"var": {}, | ||
"const": {}, | ||
"package": {}, | ||
"pragma": {}, | ||
"for": {}, | ||
"break": {}, | ||
"continue": {}, | ||
"import": {}, | ||
"true": {}, | ||
"false": {}, | ||
"interface": {}, | ||
"range": {}, | ||
"make": {}, | ||
"panic": {}, | ||
"if": {}, | ||
"else": {}, | ||
"switch": {}, | ||
"case": {}, | ||
"default": {}, | ||
"fallthrough": {}, | ||
"func": {}, | ||
"extern": {}, | ||
"return": {}, | ||
"type": {}, | ||
"table": {}, | ||
"struct": {}, | ||
"var": {}, | ||
"const": {}, | ||
"package": {}, | ||
"pragma": {}, | ||
"for": {}, | ||
"break": {}, | ||
"continue": {}, | ||
"import": {}, | ||
"true": {}, | ||
"false": {}, | ||
"interface": {}, | ||
"range": {}, | ||
"make": {}, | ||
"panic": {}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package parser | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cell-labs/cell-script/compiler/lexer" | ||
) | ||
|
||
type SwitchNode struct { | ||
baseNode | ||
Item Node | ||
Cases []*SwitchCaseNode | ||
DefaultBody []Node // can be null | ||
} | ||
|
||
type SwitchCaseNode struct { | ||
baseNode | ||
Conditions []Node | ||
Body []Node | ||
Fallthrough bool | ||
} | ||
|
||
func (s SwitchNode) String() string { | ||
return fmt.Sprintf("switch %s", s.Item) | ||
} | ||
|
||
func (s SwitchCaseNode) String() string { | ||
return fmt.Sprintf("case %+v", s.Conditions) | ||
} | ||
|
||
func (p *parser) parseSwitch() *SwitchNode { | ||
p.i++ | ||
|
||
s := &SwitchNode{ | ||
Item: p.parseOne(true), | ||
Cases: make([]*SwitchCaseNode, 0), | ||
} | ||
|
||
p.i++ | ||
p.expect(p.lookAhead(0), lexer.Item{Type: lexer.OPERATOR, Val: "{"}) | ||
p.i++ | ||
|
||
for { | ||
next := p.lookAhead(0) | ||
|
||
if next.Type == lexer.EOL { | ||
p.i++ | ||
continue | ||
} | ||
|
||
if next.Type == lexer.KEYWORD && next.Val == "case" { | ||
p.i++ | ||
switchCase := SwitchCaseNode{ | ||
Conditions: []Node{p.parseOne(true)}, | ||
} | ||
|
||
p.i++ | ||
|
||
for { | ||
curr := p.lookAhead(0) | ||
if curr.Type == lexer.OPERATOR && curr.Val == ":" { | ||
p.i++ | ||
break | ||
} | ||
|
||
if curr.Type == lexer.OPERATOR && curr.Val == "," { | ||
p.i++ | ||
switchCase.Conditions = append(append(switchCase.Conditions, | ||
p.parseOne(true), | ||
)) | ||
p.i++ | ||
continue | ||
} | ||
|
||
panic(fmt.Sprintf("Expected : or , in case. Got %+v", curr)) | ||
} | ||
|
||
var reached lexer.Item | ||
switchCase.Body, reached = p.parseUntilEither( | ||
[]lexer.Item{ | ||
{Type: lexer.OPERATOR, Val: "}"}, | ||
{Type: lexer.KEYWORD, Val: "case"}, | ||
{Type: lexer.KEYWORD, Val: "default"}, | ||
{Type: lexer.KEYWORD, Val: "fallthrough"}, | ||
}, | ||
) | ||
|
||
if reached.Type == lexer.KEYWORD && reached.Val == "fallthrough" { | ||
switchCase.Fallthrough = true | ||
p.i++ | ||
} | ||
|
||
s.Cases = append(s.Cases, &switchCase) | ||
|
||
// reached end of switch | ||
if reached.Type == lexer.OPERATOR && reached.Val == "}" { | ||
break | ||
} | ||
} | ||
|
||
if next.Type == lexer.KEYWORD && next.Val == "default" { | ||
p.i++ | ||
p.expect(p.lookAhead(0), lexer.Item{Type: lexer.OPERATOR, Val: ":"}) | ||
p.i++ | ||
|
||
body, reached := p.parseUntilEither( | ||
[]lexer.Item{ | ||
{Type: lexer.OPERATOR, Val: "}"}, | ||
{Type: lexer.KEYWORD, Val: "case"}, | ||
}, | ||
) | ||
|
||
s.DefaultBody = body | ||
|
||
// reached end of switch | ||
if reached.Type == lexer.OPERATOR && reached.Val == "}" { | ||
break | ||
} | ||
} | ||
} | ||
|
||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
package main | ||
|
||
import ( | ||
"debug" | ||
) | ||
|
||
func runIntSwitch(a uint64) { | ||
switch a { | ||
case 3: | ||
debug.Printf("three") | ||
case 6, 7, 8: | ||
debug.Printf("six, seven, eight") | ||
case 4: | ||
debug.Printf("four") | ||
fallthrough | ||
case 5: | ||
debug.Printf("five") | ||
default: | ||
debug.Printf("default") | ||
} | ||
} | ||
|
||
func runBoolSwitch(a bool) { | ||
switch a { | ||
case false: | ||
debug.Printf("false") | ||
case true: | ||
debug.Printf("true") | ||
} | ||
} | ||
|
||
func main() { | ||
a := 3 | ||
runIntSwitch(a) // three | ||
|
||
runIntSwitch(100) // default | ||
|
||
// four | ||
// five | ||
runIntSwitch(4) | ||
|
||
runIntSwitch(6) // six, seven, eight | ||
runIntSwitch(7) // six, seven, eight | ||
runIntSwitch(8) // six, seven, eight | ||
|
||
runBoolSwitch(false) // false | ||
runBoolSwitch(true) // true | ||
return 0 | ||
} |