Skip to content

Commit

Permalink
Merge pull request #75 from vapourlang/v0.0.6
Browse files Browse the repository at this point in the history
V0.0.6
  • Loading branch information
JohnCoene authored Oct 2, 2024
2 parents 5b69a28 + aa08828 commit 9f35330
Show file tree
Hide file tree
Showing 14 changed files with 698 additions and 185 deletions.
21 changes: 21 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,27 @@ func (ce *CallExpression) String() string {
return out.String()
}

type DecoratorEnvironment struct {
Token token.Item
Arguments []Argument
Type *TypeStatement
}

func (d *DecoratorEnvironment) Item() token.Item { return d.Token }
func (d *DecoratorEnvironment) expressionNode() {}
func (d *DecoratorEnvironment) TokenLiteral() string { return d.Token.Value }
func (d *DecoratorEnvironment) String() string {
var out bytes.Buffer

out.WriteString("# environment: ")
for _, arg := range d.Arguments {
out.WriteString(arg.Name)
}
out.WriteString(d.Type.String())

return out.String()
}

type DecoratorMatrix struct {
Token token.Item
Arguments []Argument
Expand Down
29 changes: 23 additions & 6 deletions environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Environment struct {
factor map[string]Factor
signature map[string]Signature
method map[string]Methods
env map[string]Env
returnType ast.Types
outer *Environment
}
Expand Down Expand Up @@ -50,15 +51,14 @@ var baseTypes = []string{
"int",
"any",
"num",
"raw",
"char",
"bool",
"null",
"na",
"na_char",
"na_int",
"na_real",
"na_complex",
"nan",
"date",
"complex",
"posixlt",
"posixct",
}

// objects
Expand All @@ -70,6 +70,7 @@ var baseObjects = []string{
"vector",
"struct",
"dataframe",
"environment",
"impliedList",
}

Expand All @@ -82,6 +83,7 @@ func NewGlobalEnvironment() *Environment {
s := make(map[string]Signature)
fct := make(map[string]Factor)
meth := make(map[string]Methods)
e := make(map[string]Env)

env := &Environment{
functions: f,
Expand All @@ -90,6 +92,7 @@ func NewGlobalEnvironment() *Environment {
class: c,
matrix: m,
signature: s,
env: e,
factor: fct,
method: meth,
outer: nil,
Expand Down Expand Up @@ -220,6 +223,7 @@ func makeTypeKey(pkg, name string) string {
}

func (e *Environment) GetType(pkg, name string) (Type, bool) {
e.LoadPackageTypes(pkg)
obj, ok := e.types[makeTypeKey(pkg, name)]
if !ok && e.outer != nil {
obj, ok = e.outer.GetType(pkg, name)
Expand Down Expand Up @@ -263,6 +267,19 @@ func (e *Environment) SetClass(name string, val Class) Class {
return val
}

func (e *Environment) GetEnv(name string) (Env, bool) {
obj, ok := e.env[name]
if !ok && e.outer != nil {
obj, ok = e.outer.GetEnv(name)
}
return obj, ok
}

func (e *Environment) SetEnv(name string, val Env) Env {
e.env[name] = val
return val
}

func (e *Environment) GetFactor(name string) (Factor, bool) {
obj, ok := e.factor[name]
if !ok && e.outer != nil {
Expand Down
5 changes: 5 additions & 0 deletions environment/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ type Factor struct {
Value *ast.DecoratorFactor
}

type Env struct {
Token token.Item
Value *ast.DecoratorEnvironment
}

type Signature struct {
Token token.Item
Value *ast.TypeFunction
Expand Down
29 changes: 9 additions & 20 deletions lexer/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ func lexDecorator(l *Lexer) stateFn {
l.emit(token.ItemDecoratorFactor)
}

if tok == "environment" {
l.emit(token.ItemDecoratorEnvironment)
}

r := l.peek(1)

if r != '(' && tok == "class" {
Expand Down Expand Up @@ -684,26 +688,6 @@ func lexIdentifier(l *Lexer) stateFn {
return lexDefault
}

if tk == "na_int" {
l.emit(token.ItemNAInteger)
return lexDefault
}

if tk == "na_char" {
l.emit(token.ItemNAString)
return lexDefault
}

if tk == "na_real" {
l.emit(token.ItemNAReal)
return lexDefault
}

if tk == "na_complex" {
l.emit(token.ItemNAComplex)
return lexDefault
}

if tk == "inf" {
l.emit(token.ItemInf)
return lexDefault
Expand Down Expand Up @@ -921,6 +905,11 @@ func lexTypeDeclaration(l *Lexer) stateFn {
return lexDefault
}

if tok == "environment" {
l.emit(token.ItemObjEnvironment)
return lexDefault
}

if tok == "dataframe" {
l.emit(token.ItemObjDataframe)
return lexDefault
Expand Down
51 changes: 45 additions & 6 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerPrefix(token.ItemDecoratorDefault, p.parseDecoratorDefault)
p.registerPrefix(token.ItemDecoratorMatrix, p.parseDecoratorMatrix)
p.registerPrefix(token.ItemDecoratorFactor, p.parseDecoratorFactor)
p.registerPrefix(token.ItemDecoratorEnvironment, p.parseDecoratorEnvironment)
p.registerPrefix(token.ItemRightSquare, p.parseSquare)
p.registerPrefix(token.ItemDoubleRightSquare, p.parseSquare)

Expand Down Expand Up @@ -267,9 +268,7 @@ func (p *Parser) parseFor() ast.Expression {
return nil
}

p.nextToken()

if !p.expectCurrent(token.ItemLet) {
if !p.expectPeek(token.ItemLet) {
return nil
}

Expand All @@ -283,7 +282,11 @@ func (p *Parser) parseFor() ast.Expression {

lit.Vector = p.parseExpression(LOWEST)

if !p.expectPeek(token.ItemRightParen) {
if p.peekTokenIs(token.ItemRightParen) {
p.nextToken()
}

if !p.curTokenIs(token.ItemRightParen) {
return nil
}

Expand Down Expand Up @@ -358,15 +361,15 @@ func (p *Parser) parseNA() ast.Expression {
return &ast.Keyword{
Token: p.curToken,
Value: "NA",
Type: &ast.Type{Name: "na"},
Type: &ast.Type{Name: ""},
}
}

func (p *Parser) parseNan() ast.Expression {
return &ast.Keyword{
Token: p.curToken,
Value: "NaN",
Type: &ast.Type{Name: "null"},
Type: &ast.Type{Name: ""},
}
}

Expand Down Expand Up @@ -553,6 +556,13 @@ func (p *Parser) parseTypeDeclaration() *ast.TypeStatement {
p.skipNewLine()
}

if p.peekTokenIs(token.ItemObjEnvironment) {
p.nextToken()
p.nextToken()
typ.Object = "environment"
p.skipNewLine()
}

if p.peekTokenIs(token.ItemObjObject) {
p.nextToken()
p.nextToken()
Expand All @@ -578,6 +588,9 @@ func (p *Parser) parseTypeAttributes() []*ast.TypeAttributesStatement {
for !p.peekTokenIs(token.ItemRightCurly) && !p.peekTokenIs(token.ItemEOF) {
p.nextToken()
attrs = append(attrs, p.parseTypeAttribute())
if p.curTokenIs(token.ItemRightCurly) {
return attrs
}
}

p.nextToken()
Expand Down Expand Up @@ -1142,6 +1155,32 @@ func (p *Parser) parseDecoratorDefault() ast.Expression {
return dec
}

func (p *Parser) parseDecoratorEnvironment() ast.Expression {
dec := &ast.DecoratorEnvironment{
Token: p.curToken,
}

if !p.expectPeek(token.ItemLeftParen) {
return nil
}

dec.Arguments = p.parseCallArguments()

p.nextToken()

if !p.expectPeek(token.ItemNewLine) {
return nil
}

if !p.expectPeek(token.ItemTypesDecl) {
return nil
}

dec.Type = p.parseTypeDeclaration()

return dec
}

func (p *Parser) parseDecoratorFactor() ast.Expression {
dec := &ast.DecoratorFactor{
Token: p.curToken,
Expand Down
74 changes: 52 additions & 22 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,28 +234,6 @@ func TestRange(t *testing.T) {
fmt.Println(prog.String())
}

func TestFor(t *testing.T) {
fmt.Println("---------------------------------------------------------- for")
code := `for(let i:int = 1 in 1..nrow(df)) {
print(i)
}
func foo(...: int): int {
sum(...)
}
let x: int = (1, 20, 23) `

l := lexer.NewTest(code)

l.Run()
p := New(l)

prog := p.Run()

fmt.Println(prog.String())
}

func TestWhile(t *testing.T) {
fmt.Println("---------------------------------------------------------- while")
code := `while(i < 10) {
Expand Down Expand Up @@ -759,3 +737,55 @@ func TestTypePackage(t *testing.T) {

fmt.Println(prog.String())
}

func TestInline(t *testing.T) {
fmt.Println("---------------------------------------------------------- type package")
code := `type t: object { x: int, y: num }`

l := lexer.NewTest(code)

l.Run()
p := New(l)

prog := p.Run()

if len(p.errors) > 0 {
p.errors.Print()
return
}

fmt.Println(prog.String())
}

func TestFor(t *testing.T) {
fmt.Println("---------------------------------------------------------- for")
code := `for(let i: int in 1..nrow(df)) {
print(i)
}
for(let x: int in 1..10) {
print(x)
}
const zap: int = (10, 20, 30)
for(let y:int in zap) {
print(y)
}
func foo(...: int): int {
sum(...)
}
let x: int = (1, 20, 23) `

l := lexer.NewTest(code)

l.Run()
p := New(l)

prog := p.Run()

fmt.Println(prog.String())
}
Loading

0 comments on commit 9f35330

Please sign in to comment.