Created basic test for parser

This commit is contained in:
Sasha Koshka 2022-08-12 11:55:17 -05:00
parent f4f19a809a
commit 2019c67bbb
5 changed files with 93 additions and 3 deletions

View File

@ -11,18 +11,25 @@ type ParsingOperation struct {
token lexer.Token token lexer.Token
tokens []lexer.Token tokens []lexer.Token
tokenIndex int tokenIndex int
tree *SyntaxTree
} }
// Parse reads the files located in the module specified by modulePath, and // Parse reads the files located in the module specified by modulePath, and
// converts them into an abstract syntax tree. // converts them into an abstract syntax tree.
func Parse (modulePath string) (tree *SyntaxTree, err error) { func Parse (modulePath string) (tree *SyntaxTree, err error) {
parser := ParsingOperation { modulePath: modulePath } parser := ParsingOperation { modulePath: modulePath }
tree, err = parser.parse() err = parser.parse()
tree = parser.tree
return return
} }
// parse runs the parsing operation. // parse runs the parsing operation.
func (parser *ParsingOperation) parse () (tree *SyntaxTree, err error) { func (parser *ParsingOperation) parse () (err error) {
if parser.tree == nil {
parser.tree = &SyntaxTree { }
}
if parser.modulePath[len(parser.modulePath) - 1] != '/' { if parser.modulePath[len(parser.modulePath) - 1] != '/' {
parser.modulePath += "/" parser.modulePath += "/"
} }

33
parser/parser_test.go Normal file
View File

@ -0,0 +1,33 @@
package parser
import "reflect"
import "testing"
func checkTree (modulePath string, correct *SyntaxTree, test *testing.T) {
tree, err := Parse(modulePath)
if err != nil {
test.Log("returned error:")
test.Log(err.Error())
test.Fail()
return
}
if !reflect.DeepEqual(tree, correct) {
test.Log("trees not equal")
test.Fail()
return
}
}
func TestMeta (test *testing.T) {
checkTree("../tests/parser/meta",&SyntaxTree {
license: "GPLv3",
author: "Sasha Koshka",
requires: []string {
"someModule",
"otherModule",
},
}, test)
}

View File

@ -4,5 +4,8 @@ package parser
// can be expected to be syntactically correct, but it might not be semantically // can be expected to be syntactically correct, but it might not be semantically
// correct (because it has not been analyzed yet.) // correct (because it has not been analyzed yet.)
type SyntaxTree struct { type SyntaxTree struct {
license string
author string
requires []string
} }

View File

@ -0,0 +1,41 @@
:arf
author "Sasha Koshka"
license "GPLv3"
require "io"
---
# this is a global variable
data wn helloText:String "Hello, world!"
# this is a struct definition
type rr Greeter:Obj
# "Hi." is a string constant. all Greeters will be initialized with a
# pointer to it. I don't know really it depends on what I decide that
# a String type even is.
wr text:String "Hi."
"sdfdsf" "ahh"
"asdf"
# this is a function
func rr main
> argc:Int
> argv:{String}
< status:Int 0
---
let greeter:Greeter:mut
greeter.setText helloText
greeter.greet
# this is a member function
func rr greet
@ greeter:{Greeter}
---
io.println greeter.text
# this is mutator member function
func rr setText
@ greeter:{Greeter}
> text:String
---
greeter.text.set text

View File

@ -0,0 +1,6 @@
:arf
author "Sasha Koshka"
license "GPLv3"
require "someModule"
require "otherModule"
---