Compare commits

...
This repository has been archived on 2022-08-30. You can view files and clone it, but cannot push or open issues or pull requests.

8 Commits

Author SHA1 Message Date
Sasha Koshka 06a99ce232 Implement new tokens
I think this is all of them. The test will tell.
2022-08-30 01:02:49 -04:00
Sasha Koshka 1c2194b68a Added text descriptions for new tokens 2022-08-25 23:21:00 -04:00
Sasha Koshka 453a596587 Added test case for new tokens 2022-08-25 23:17:42 -04:00
Sasha Koshka c3b6330b22 Added base function parsing 2022-08-25 20:01:12 -04:00
Sasha Koshka 723b506005 Added test code for function sections 2022-08-25 16:08:18 -04:00
Sasha Koshka 6bbee2e13b Created comprehensive test case 2022-08-25 15:46:35 -04:00
Sasha Koshka 9fd285920b Began writing test case for function sections 2022-08-25 13:31:09 -04:00
Sasha Koshka e630ec6f04 Added function section to tree 2022-08-25 12:02:43 -04:00
9 changed files with 368 additions and 5 deletions

View File

@ -253,14 +253,26 @@ func (lexer *LexingOperation) tokenizeSymbolBeginning () (err error) {
lexer.addToken(token) lexer.addToken(token)
case '%': case '%':
token := lexer.newToken() token := lexer.newToken()
token.kind = TokenKindPercent
lexer.addToken(token)
err = lexer.nextRune() err = lexer.nextRune()
if err != nil { return }
token.kind = TokenKindPercent
if lexer.char == '=' {
token.kind = TokenKindPercentAssignment
err = lexer.nextRune()
token.location.SetWidth(2)
}
lexer.addToken(token)
case '~': case '~':
token := lexer.newToken() token := lexer.newToken()
token.kind = TokenKindTilde
lexer.addToken(token)
err = lexer.nextRune() err = lexer.nextRune()
if err != nil { return }
token.kind = TokenKindTilde
if lexer.char == '=' {
token.kind = TokenKindTildeAssignment
err = lexer.nextRune()
token.location.SetWidth(2)
}
lexer.addToken(token)
case '=': case '=':
token := lexer.newToken() token := lexer.newToken()
token.kind = TokenKindEqualTo token.kind = TokenKindEqualTo
@ -275,6 +287,11 @@ func (lexer *LexingOperation) tokenizeSymbolBeginning () (err error) {
token.kind = TokenKindLShift token.kind = TokenKindLShift
err = lexer.nextRune() err = lexer.nextRune()
token.location.SetWidth(2) token.location.SetWidth(2)
if lexer.char == '=' {
token.kind = TokenKindLShiftAssignment
err = lexer.nextRune()
token.location.SetWidth(2)
}
} else if lexer.char == '=' { } else if lexer.char == '=' {
token.kind = TokenKindLessThanEqualTo token.kind = TokenKindLessThanEqualTo
err = lexer.nextRune() err = lexer.nextRune()
@ -290,6 +307,11 @@ func (lexer *LexingOperation) tokenizeSymbolBeginning () (err error) {
token.kind = TokenKindRShift token.kind = TokenKindRShift
err = lexer.nextRune() err = lexer.nextRune()
token.location.SetWidth(2) token.location.SetWidth(2)
if lexer.char == '=' {
token.kind = TokenKindRShiftAssignment
err = lexer.nextRune()
token.location.SetWidth(2)
}
} else if lexer.char == '=' { } else if lexer.char == '=' {
token.kind = TokenKindGreaterThanEqualTo token.kind = TokenKindGreaterThanEqualTo
err = lexer.nextRune() err = lexer.nextRune()
@ -305,6 +327,10 @@ func (lexer *LexingOperation) tokenizeSymbolBeginning () (err error) {
token.kind = TokenKindLogicalOr token.kind = TokenKindLogicalOr
err = lexer.nextRune() err = lexer.nextRune()
token.location.SetWidth(2) token.location.SetWidth(2)
} else if lexer.char == '=' {
token.kind = TokenKindBinaryOrAssignment
err = lexer.nextRune()
token.location.SetWidth(2)
} }
lexer.addToken(token) lexer.addToken(token)
case '&': case '&':
@ -316,6 +342,10 @@ func (lexer *LexingOperation) tokenizeSymbolBeginning () (err error) {
token.kind = TokenKindLogicalAnd token.kind = TokenKindLogicalAnd
err = lexer.nextRune() err = lexer.nextRune()
token.location.SetWidth(2) token.location.SetWidth(2)
} else if lexer.char == '=' {
token.kind = TokenKindBinaryAndAssignment
err = lexer.nextRune()
token.location.SetWidth(2)
} }
lexer.addToken(token) lexer.addToken(token)
default: default:

View File

@ -152,19 +152,27 @@ func TestTokenizeAll (test *testing.T) {
quickToken(1, TokenKindAt, nil), quickToken(1, TokenKindAt, nil),
quickToken(1, TokenKindExclamation, nil), quickToken(1, TokenKindExclamation, nil),
quickToken(1, TokenKindPercent, nil), quickToken(1, TokenKindPercent, nil),
quickToken(2, TokenKindPercentAssignment, nil),
quickToken(1, TokenKindTilde, nil), quickToken(1, TokenKindTilde, nil),
quickToken(2, TokenKindTildeAssignment, nil),
quickToken(1, TokenKindEqualTo, nil), quickToken(1, TokenKindEqualTo, nil),
quickToken(2, TokenKindNotEqualTo, nil), quickToken(2, TokenKindNotEqualTo, nil),
quickToken(1, TokenKindLessThan, nil), quickToken(1, TokenKindLessThan, nil),
quickToken(2, TokenKindLessThanEqualTo, nil), quickToken(2, TokenKindLessThanEqualTo, nil),
quickToken(2, TokenKindLShift, nil), quickToken(2, TokenKindLShift, nil),
quickToken(3, TokenKindLShiftAssignment, nil),
quickToken(1, TokenKindGreaterThan, nil), quickToken(1, TokenKindGreaterThan, nil),
quickToken(2, TokenKindGreaterThanEqualTo, nil), quickToken(2, TokenKindGreaterThanEqualTo, nil),
quickToken(2, TokenKindRShift, nil), quickToken(2, TokenKindRShift, nil),
quickToken(3, TokenKindRShiftAssignment, nil),
quickToken(1, TokenKindBinaryOr, nil), quickToken(1, TokenKindBinaryOr, nil),
quickToken(2, TokenKindBinaryOrAssignment, nil),
quickToken(2, TokenKindLogicalOr, nil), quickToken(2, TokenKindLogicalOr, nil),
quickToken(1, TokenKindBinaryAnd, nil), quickToken(1, TokenKindBinaryAnd, nil),
quickToken(2, TokenKindBinaryAndAssignment, nil),
quickToken(2, TokenKindLogicalAnd, nil), quickToken(2, TokenKindLogicalAnd, nil),
quickToken(1, TokenKindBinaryXor, nil),
quickToken(2, TokenKindBinaryXorAssignment, nil),
quickToken(1, TokenKindNewline, nil), quickToken(1, TokenKindNewline, nil),
) )
} }

View File

@ -43,20 +43,28 @@ const (
TokenKindAt TokenKindAt
TokenKindExclamation TokenKindExclamation
TokenKindPercent TokenKindPercent
TokenKindPercentAssignment
TokenKindTilde TokenKindTilde
TokenKindTildeAssignment
TokenKindEqualTo TokenKindEqualTo
TokenKindNotEqualTo TokenKindNotEqualTo
TokenKindLessThanEqualTo TokenKindLessThanEqualTo
TokenKindLessThan TokenKindLessThan
TokenKindLShift TokenKindLShift
TokenKindLShiftAssignment
TokenKindGreaterThan TokenKindGreaterThan
TokenKindGreaterThanEqualTo TokenKindGreaterThanEqualTo
TokenKindRShift TokenKindRShift
TokenKindRShiftAssignment
TokenKindBinaryOr TokenKindBinaryOr
TokenKindBinaryOrAssignment
TokenKindLogicalOr TokenKindLogicalOr
TokenKindBinaryAnd TokenKindBinaryAnd
TokenKindBinaryAndAssignment
TokenKindLogicalAnd TokenKindLogicalAnd
TokenKindBinaryXor
TokenKindBinaryXorAssignment
) )
// Token represents a single token. It holds its location in the file, as well // Token represents a single token. It holds its location in the file, as well
@ -175,8 +183,12 @@ func (tokenKind TokenKind) Describe () (description string) {
description = "Exclamation" description = "Exclamation"
case TokenKindPercent: case TokenKindPercent:
description = "Percent" description = "Percent"
case TokenKindPercentAssignment:
description = "PercentAssignment"
case TokenKindTilde: case TokenKindTilde:
description = "Tilde" description = "Tilde"
case TokenKindTildeAssignment:
description = "TildeAssignment"
case TokenKindEqualTo: case TokenKindEqualTo:
description = "EqualTo" description = "EqualTo"
case TokenKindNotEqualTo: case TokenKindNotEqualTo:
@ -187,20 +199,32 @@ func (tokenKind TokenKind) Describe () (description string) {
description = "LessThanEqualTo" description = "LessThanEqualTo"
case TokenKindLShift: case TokenKindLShift:
description = "LShift" description = "LShift"
case TokenKindLShiftAssignment:
description = "LShiftAssignment"
case TokenKindGreaterThan: case TokenKindGreaterThan:
description = "GreaterThan" description = "GreaterThan"
case TokenKindGreaterThanEqualTo: case TokenKindGreaterThanEqualTo:
description = "GreaterThanEqualTo" description = "GreaterThanEqualTo"
case TokenKindRShift: case TokenKindRShift:
description = "RShift" description = "RShift"
case TokenKindRShiftAssignment:
description = "RShiftAssignment"
case TokenKindBinaryOr: case TokenKindBinaryOr:
description = "BinaryOr" description = "BinaryOr"
case TokenKindBinaryOrAssignment:
description = "BinaryOrAssignment"
case TokenKindLogicalOr: case TokenKindLogicalOr:
description = "LogicalOr" description = "LogicalOr"
case TokenKindBinaryAnd: case TokenKindBinaryAnd:
description = "BinaryAnd" description = "BinaryAnd"
case TokenKindBinaryAndAssignment:
description = "BinaryAndAssignment"
case TokenKindLogicalAnd: case TokenKindLogicalAnd:
description = "LogicalAnd" description = "LogicalAnd"
case TokenKindBinaryXor:
description = "BinaryXor"
case TokenKindBinaryXorAssignment:
description = "BinaryXorAssignment"
} }
return return

View File

@ -57,6 +57,14 @@ func (parser *ParsingOperation) parseBody () (err error) {
parser.tree.enumSections[section.name] = section parser.tree.enumSections[section.name] = section
if err != nil { return } if err != nil { return }
case "func": case "func":
var section *FuncSection
section, err = parser.parseFuncSection()
if parser.tree.funcSections == nil {
parser.tree.funcSections =
make(map[string] *FuncSection)
}
parser.tree.funcSections[section.name] = section
if err != nil { return }
default: default:
err = parser.token.NewError ( err = parser.token.NewError (
"unknown section type \"" + sectionType + "\"", "unknown section type \"" + sectionType + "\"",

30
parser/func.go Normal file
View File

@ -0,0 +1,30 @@
package parser
import "git.tebibyte.media/sashakoshka/arf/types"
import "git.tebibyte.media/sashakoshka/arf/lexer"
// import "git.tebibyte.media/sashakoshka/arf/infoerr"
// parseFunc parses a function section.
func (parser *ParsingOperation) parseFuncSection () (
section *FuncSection,
err error,
) {
err = parser.expect(lexer.TokenKindName)
if err != nil { return }
section = &FuncSection { location: parser.token.Location() }
err = parser.nextToken(lexer.TokenKindPermission)
if err != nil { return }
section.permission = parser.token.Value().(types.Permission)
err = parser.nextToken(lexer.TokenKindName)
if err != nil { return }
section.name = parser.token.Value().(string)
err = parser.nextToken(lexer.TokenKindNewline)
if err != nil { return }
return
}

111
parser/func_test.go Normal file
View File

@ -0,0 +1,111 @@
package parser
import "testing"
func TestFunc (test *testing.T) {
checkTree ("../tests/parser/func",
`:arf
---
func ro aBasicExternal
> someInput:Int:mut
< someOutput:Int 4
---
external
func ro bMethod
@ bird:{Bird}
> someInput:Int:mut
< someOutput:Int 4
---
external
func ro cBasicPhrases
---
[fn 329 983 09]
[fn 329 983 09]
[fn 329 983 091]
[fn [gn 329 983 091] 123]
func ro dArgumentTypes
---
[bird tree butterfly.wing "hello world" grass:{Int:mut 8}]
func ro eMath
[> x:Int]
[> y:Int]
[< z:Int]
[---]
[++ x]
[-- y]
[set z [+ [* 0392 00] 98 x [/ 9832 y] 930]]
[! true]
[~ 0b01]
[% 873 32]
[= 5 5]
[!= 4 4]
[<= 4 98]
[< 4 98]
[<< 0x0F 4]
[>= 98 4]
[> 98 4]
[>> 0xF0 4]
[| 0b01 0b10]
[& 0b110 0b011]
[&& true true]
[|| true false]
func ro fReturnDirection
< err:Error
---
[someFunc 498 2980 90] -> thing:Int err
[otherFunc] -> thing err:Error
[fn 329 983 091] -> thing:Int err
func ro gControlFlow
---
[if condition]
[something]
[if condition]
[something]
[elseif]
[otherThing]
[else]
[finalThing]
[while [< x 432]]
[something]
[switch value]
[: 324]
[something]
[: 93284]
otherThing
[: 9128 34738 7328]
multipleCases
[:]
[defaultThing]
[for index:Size element:Int someArray]
[something]
[someNextThing]
[justMakingSureBlockParsingWorks]
[if condition]
[if condition]
[nestedThing]
[else]
[otherThing]
[else]
[if condition]
[nestedThing]
[else]
[otherThing]
func hSetPhrase
---
[set x:Int 3]
[set y:{Int} [. x]]
[set z:{Int 8}]
398
9
2309
983
-2387
478
555
123
[set bird:Bird]
.that
.whenYou 99999
.this 324
`, test)
}

View File

@ -16,6 +16,7 @@ type SyntaxTree struct {
enumSections map[string] *EnumSection enumSections map[string] *EnumSection
faceSections map[string] *FaceSection faceSections map[string] *FaceSection
dataSections map[string] *DataSection dataSections map[string] *DataSection
funcSections map[string] *FuncSection
} }
// Identifier represents a chain of arguments separated by a dot. // Identifier represents a chain of arguments separated by a dot.
@ -229,3 +230,20 @@ type FaceSection struct {
permission types.Permission permission types.Permission
behaviors map[string] FaceBehavior behaviors map[string] FaceBehavior
} }
// Block represents a scoped/indented block of code.
// TODO: blocks will not directly nest. nested blocks will be stored as a part
// of certain control flow statements.
type Block []Phrase
// FuncSection represents a function section.
type FuncSection struct {
location file.Location
name string
permission types.Permission
receiver *Declaration
inputs []Declaration
outputs []Declaration
root *Block
}

View File

@ -1,3 +1,3 @@
:arf :arf
--- rw -> -349820394 932748397 239485.37520 "hello world!\n" 'E' helloWorld:.,..[]{} --- rw -> -349820394 932748397 239485.37520 "hello world!\n" 'E' helloWorld:.,..[]{}
+ - ++ -- * / @ ! % ~ = != < <= << > >= >> | || & && + - ++ -- * / @ ! % %= ~ ~= = != < <= << <<= > >= >> >>= | |= || & &= && ^ ^=

134
tests/parser/func/main.arf Normal file
View File

@ -0,0 +1,134 @@
:arf
---
func ro aBasicExternal
> someInput:Int:mut
< someOutput:Int 4
---
external
func ro bMethod
@ bird:{Bird}
> someInput:Int:mut
< someOutput:Int 4
---
external
func ro cBasicPhrases
---
fn 329 983 09
[fn 329 983 09]
[fn
329
983
091]
fn [gn
329 983
091] 123
func ro dArgumentTypes
---
[bird tree butterfly.wing "hello world"
grass:{Int:mut 8}]
func ro eMath
> x:Int
> y:Int
< z:Int
---
++ x
-- y
set z [+ [* 0392 00] 98 x [/ 9832 y] 930]
# TODO: need tokens ~=
! true
~ 0b01
# ~= x
% 873 32
= 5 5
!= 4 4
<= 4 98
< 4 98
<< 0x0F 4
# <<= x 4
>= 98 4
> 98 4
>> 0xF0 4
# >>= x 4
| 0b01 0b10
# |= x 0b10
& 0b110 0b011
# &= x 0b011
&& true true
|| true false
func ro fReturnDirection
< err:Error
---
someFunc 498 2980 90 -> thing:Int err
otherFunc -> thing err:Error
[fn
329
983
091] -> thing:Int err
func ro gControlFlow
---
if condition
something
if condition
something
elseif
[otherThing]
else
finalThing
while [< x 432]
something
switch value
: 324
something
[: 93284]
otherThing
: 9128 34738 7328
multipleCases
:
[defaultThing]
for index:Size element:Int someArray
something
someNextThing
justMakingSureBlockParsingWorks
[if condition]
if condition
nestedThing
else
otherThing
else
if condition
nestedThing
else
otherThing
func hSetPhrase
---
set x:Int 3
# TODO: this should be the "location of" phrase. update other things to
# match.
set y:{Int} [. x]
set z:{Int 8}
398 9 2309 983 -2387
478 555 123
set bird:Bird
.that
.whenYou 99999
.this 324