Skip to content

Commit cd8846c

Browse files
committed
Now using func keyword
1 parent fb55411 commit cd8846c

File tree

5 files changed

+38
-33
lines changed

5 files changed

+38
-33
lines changed

lex/token.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
EqualToken
1818
LeftCurlyBraceToken
1919
RightCurlyBraceToken
20-
FunctionKeywordToken
20+
FuncKeywordToken
2121
GlobalKeywordToken
2222
TypeKeywordToken
2323
PointerToken
@@ -37,7 +37,7 @@ var tokenNames = map[TokenType]string{
3737
RightCurlyBraceToken: "Right Curly Brace",
3838
GlobalKeywordToken: "Global Keyword",
3939
TypeKeywordToken: "Type Keyword",
40-
FunctionKeywordToken: "Function Keyword",
40+
FuncKeywordToken: "Func Keyword",
4141
PointerToken: "Pointer",
4242
RepeatToken: "Repeat",
4343
OperatorToken: "Operator",

lex/tokenizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewTokenizer() Tokenizer {
3232
KeywordTokenizer{"=", EqualToken},
3333
KeywordTokenizer{"{", LeftCurlyBraceToken},
3434
KeywordTokenizer{"}", RightCurlyBraceToken},
35-
KeywordTokenizer{"function", FunctionKeywordToken},
35+
KeywordTokenizer{"func", FuncKeywordToken},
3636
KeywordTokenizer{"global", GlobalToken},
3737
KeywordTokenizer{"type", TypeKeywordToken},
3838
WordTokenizer{OperatorToken},

lex/tokenizer_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ func assertExpectedTokens(t *testing.T, expected []tknDesc, actual []lex.Token,
2626

2727
func TestAddOne(t *testing.T) {
2828
code :=
29-
`function $32 @addOne $32 %x =
29+
`func $32 @addOne $32 %x {
3030
%0 = add %x $32 #1
3131
ret %0
32+
}
3233
`
3334

3435
expected := []tknDesc{
35-
{"function", lex.FunctionKeywordToken},
36+
{"func", lex.FuncKeywordToken},
3637
{"$32", lex.TypeToken},
3738
{"@addOne", lex.GlobalToken},
3839
{"$32", lex.TypeToken},
3940
{"%x", lex.RegisterToken},
40-
{"=", lex.EqualToken},
41+
{"{", lex.LeftCurlyBraceToken},
4142
{"", lex.SeparatorToken},
4243
{"%0", lex.RegisterToken},
4344
{"=", lex.EqualToken},
@@ -49,6 +50,8 @@ func TestAddOne(t *testing.T) {
4950
{"ret", lex.OperatorToken},
5051
{"%0", lex.RegisterToken},
5152
{"", lex.SeparatorToken},
53+
{"}", lex.RightCurlyBraceToken},
54+
{"", lex.SeparatorToken},
5255
}
5356

5457
view := source.NewSourceView(code)
@@ -61,7 +64,7 @@ func TestAddOne(t *testing.T) {
6164

6265
func TestPow(t *testing.T) {
6366
code :=
64-
`function $32 @pow $32 %base $32 %exp =
67+
`func $32 @pow $32 %base $32 %exp {
6568
jz %exp .end
6669
6770
.recurse
@@ -80,17 +83,17 @@ func TestPow(t *testing.T) {
8083
.end
8184
%res.3 = phi . %base .even_base %res.2
8285
ret %res.3
83-
`
86+
}`
8487

8588
expected := []tknDesc{
86-
{"function", lex.FunctionKeywordToken},
89+
{"func", lex.FuncKeywordToken},
8790
{"$32", lex.TypeToken},
8891
{"@pow", lex.GlobalToken},
8992
{"$32", lex.TypeToken},
9093
{"%base", lex.RegisterToken},
9194
{"$32", lex.TypeToken},
9295
{"%exp", lex.RegisterToken},
93-
{"=", lex.EqualToken},
96+
{"{", lex.LeftCurlyBraceToken},
9497
{"", lex.SeparatorToken},
9598

9699
{"jz", lex.OperatorToken},
@@ -174,6 +177,8 @@ func TestPow(t *testing.T) {
174177
{"ret", lex.OperatorToken},
175178
{"%res.3", lex.RegisterToken},
176179
{"", lex.SeparatorToken},
180+
181+
{"}", lex.RightCurlyBraceToken},
177182
}
178183

179184
view := source.NewSourceView(code)

parse/file_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// The purpose of the test is to verify the structure of "simple" source file.
1414
func TestSingleFunction(t *testing.T) {
15-
src := `function $32 @add $32 %x $32 %y {
15+
src := `func $32 @add $32 %x $32 %y {
1616
%res = add %x %y
1717
ret %res
1818
}`
@@ -24,39 +24,39 @@ func TestSingleFunction(t *testing.T) {
2424
{
2525
UnmanagedSourceView: srcView,
2626
Declaration: parse.FunctionDeclarationNode{
27-
UnmanagedSourceView: srcView.Subview(9, 31),
28-
Identifier: srcView.Subview(13, 17),
27+
UnmanagedSourceView: srcView.Subview(5, 27),
28+
Identifier: srcView.Subview(9, 13),
2929
Parameters: []parse.ParameterNode{
3030
{
31-
Type: parse.TypeNode{srcView.Subview(18, 21)},
32-
Register: parse.RegisterNode{srcView.Subview(22, 24)},
31+
Type: parse.TypeNode{srcView.Subview(14, 17)},
32+
Register: parse.RegisterNode{srcView.Subview(18, 20)},
3333
},
3434
{
35-
Type: parse.TypeNode{srcView.Subview(25, 28)},
36-
Register: parse.RegisterNode{srcView.Subview(29, 31)},
35+
Type: parse.TypeNode{srcView.Subview(21, 24)},
36+
Register: parse.RegisterNode{srcView.Subview(25, 27)},
3737
},
3838
},
3939
Returns: []parse.TypeNode{
40-
{srcView.Subview(9, 12)},
40+
{srcView.Subview(5, 8)},
4141
},
4242
},
4343
Instructions: parse.BlockNode[parse.InstructionNode]{
44-
UnmanagedSourceView: srcView.Subview(32, 73),
44+
UnmanagedSourceView: srcView.Subview(28, 69),
4545
Nodes: []parse.InstructionNode{
4646
{
47-
Operator: srcView.Subview(42, 45),
47+
Operator: srcView.Subview(38, 41),
4848
Arguments: []parse.ArgumentNode{
49-
parse.RegisterNode{srcView.Subview(46, 48)},
50-
parse.RegisterNode{srcView.Subview(49, 51)},
49+
parse.RegisterNode{srcView.Subview(42, 44)},
50+
parse.RegisterNode{srcView.Subview(45, 47)},
5151
},
5252
Targets: []parse.RegisterNode{
53-
{srcView.Subview(35, 39)},
53+
{srcView.Subview(31, 35)},
5454
},
5555
},
5656
{
57-
Operator: srcView.Subview(53, 56),
57+
Operator: srcView.Subview(49, 52),
5858
Arguments: []parse.ArgumentNode{
59-
parse.RegisterNode{srcView.Subview(57, 61)},
59+
parse.RegisterNode{srcView.Subview(53, 57)},
6060
},
6161
},
6262
},
@@ -76,16 +76,16 @@ func TestSingleFunction(t *testing.T) {
7676
}
7777

7878
func TestFileParserTwoFunctionsNoExtraSeparator(t *testing.T) {
79-
src := `function @first { ret }
80-
function @second {
79+
src := `func @first { ret }
80+
func @second {
8181
ret
8282
}`
8383

84-
expected := `function @first {
84+
expected := `func @first {
8585
ret
8686
}
8787
88-
function @second {
88+
func @second {
8989
ret
9090
}
9191
`
@@ -94,7 +94,7 @@ function @second {
9494

9595
func TestFileWithLabels(t *testing.T) {
9696
src := `
97-
function $32 @fib $i32 %n {
97+
func $32 @fib $i32 %n {
9898
9999
jle %n $32 #1 .return
100100
%n = dec %n
@@ -108,7 +108,7 @@ function $32 @fib $i32 %n {
108108
}
109109
`
110110

111-
expected := `function $32 @fib $i32 %n {
111+
expected := `func $32 @fib $i32 %n {
112112
jle %n $32 #1 .return
113113
%n = dec %n
114114
%a = call @fib %n

parse/function.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (n FunctionNode) View() source.UnmanagedSourceView {
1616
}
1717

1818
func (n FunctionNode) String(ctx source.SourceContext) string {
19-
s := "function " + n.Declaration.String(ctx)
19+
s := "func " + n.Declaration.String(ctx)
2020
if len(n.Instructions.Nodes) > 0 {
2121
s += " " + n.Instructions.String(ctx)
2222
}
@@ -33,7 +33,7 @@ func (FunctionParser) String() string {
3333
}
3434

3535
func (FunctionParser) parseFunctionKeyword(v *TokenView, node *FunctionNode) ParsingError {
36-
kw, err := v.ConsumeTokenIgnoreSeparator(lex.FunctionKeywordToken)
36+
kw, err := v.ConsumeTokenIgnoreSeparator(lex.FuncKeywordToken)
3737
if err != nil {
3838
return err
3939
}

0 commit comments

Comments
 (0)