-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update parser_test.go * can parse classes * Add instances * init tests * update
- Loading branch information
Showing
18 changed files
with
374 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
kalasi Munthu { | ||
ndondomeko ndiNdani() { | ||
console.lemba("Mariya Malizeni"); | ||
} | ||
} | ||
|
||
Munthu munthu = Munthu(); | ||
munthu.ndiNdani(); |
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,31 @@ | ||
package ast | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/sevenreup/chewa/src/token" | ||
) | ||
|
||
type ClassStatement struct { | ||
Expression | ||
Token token.Token | ||
Name *Identifier | ||
Super *Identifier | ||
Body *BlockStatement | ||
} | ||
|
||
func (class *ClassStatement) expressionNode() {} | ||
func (class *ClassStatement) TokenLiteral() string { return class.Token.Literal } | ||
func (class *ClassStatement) String() string { | ||
var out bytes.Buffer | ||
out.WriteString("ndondomeko ") | ||
out.WriteString(class.Name.String()) | ||
if class.Super != nil { | ||
out.WriteString(" ndi ") | ||
out.WriteString(class.Super.String()) | ||
} | ||
out.WriteString(" {\n") | ||
out.WriteString(class.Body.String()) | ||
out.WriteString("\n}") | ||
return out.String() | ||
} |
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,46 @@ | ||
package chewa | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/sevenreup/chewa/src/evaluator" | ||
"github.com/sevenreup/chewa/src/object" | ||
"github.com/sevenreup/chewa/src/utils" | ||
|
||
"github.com/sevenreup/chewa/src/lexer" | ||
"github.com/sevenreup/chewa/src/parser" | ||
) | ||
|
||
type Chewa struct { | ||
file string | ||
Environment *object.Environment | ||
} | ||
|
||
func New(file string) *Chewa { | ||
chewa := &Chewa{ | ||
file: file, | ||
Environment: object.NewEnvironment(), | ||
} | ||
chewa.registerEvaluator() | ||
return chewa | ||
} | ||
|
||
func (c *Chewa) Run() { | ||
file, err := os.ReadFile(c.file) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
l := lexer.New(file) | ||
p := parser.New(l) | ||
env := object.NewEnvironment() | ||
program := p.ParseProgram() | ||
if len(p.Errors()) != 0 { | ||
utils.PrintParserErrors(os.Stdout, p.Errors()) | ||
} | ||
evaluator.Eval(program, env) | ||
} | ||
|
||
func (c *Chewa) registerEvaluator() { | ||
object.RegisterEvaluator(evaluator.Eval) | ||
} |
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,25 @@ | ||
package evaluator | ||
|
||
import ( | ||
"github.com/sevenreup/chewa/src/ast" | ||
"github.com/sevenreup/chewa/src/object" | ||
) | ||
|
||
func evaluateClass(node *ast.ClassStatement, env *object.Environment) object.Object { | ||
classEnv := object.NewEnclosedEnvironment(env) | ||
|
||
class := &object.Class{ | ||
Name: node.Name, | ||
Env: classEnv, | ||
} | ||
|
||
result := Eval(node.Body, classEnv) | ||
|
||
if isError(result) { | ||
return result | ||
} | ||
|
||
env.Set(class.Name.Value, class) | ||
|
||
return class | ||
} |
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
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,35 @@ | ||
package object | ||
|
||
import "github.com/sevenreup/chewa/src/ast" | ||
|
||
const CLASS_OBJ = "CLASS" | ||
|
||
type Class struct { | ||
Object | ||
Name *ast.Identifier | ||
Env *Environment | ||
} | ||
|
||
func (c *Class) Type() ObjectType { return CLASS_OBJ } | ||
|
||
func (c *Class) Inspect() string { | ||
return "class " + c.Name.String() | ||
} | ||
|
||
func (i *Class) Method(method string, args []Object) (Object, bool) { | ||
switch method { | ||
case "new": | ||
instance := &Instance{Class: i, Env: NewEnclosedEnvironment(i.Env)} | ||
|
||
if ok := i.Env.Has("constructor"); ok { | ||
result := instance.Call("constructor", args) | ||
|
||
if result != nil && result.Type() == ERROR_OBJ { | ||
return result, false | ||
} | ||
} | ||
|
||
return instance, true | ||
} | ||
return nil, false | ||
} |
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,44 @@ | ||
package object | ||
|
||
const INSTANCE_OBJ = "INSTANCE" | ||
|
||
type Instance struct { | ||
Class *Class | ||
Env *Environment | ||
} | ||
|
||
func (i *Instance) Type() ObjectType { return INSTANCE_OBJ } | ||
|
||
func (i *Instance) Inspect() string { | ||
return i.Class.Name.String() | ||
} | ||
|
||
func (i *Instance) Method(method string, args []Object) (Object, bool) { | ||
return nil, true | ||
} | ||
|
||
func (i *Instance) Call(method string, args []Object) Object { | ||
function, ok := i.Env.Get(method) | ||
if !ok { | ||
return NewError("undefined method %s for %s", method, i.Class.Name.String()) | ||
} | ||
methodFunction, ok := function.(*Function) | ||
if !ok { | ||
return NewError("undefined method %s for %s", method, i.Class.Name.String()) | ||
} | ||
|
||
methodEnv := createNewMethodInstanceEnvironment(methodFunction, args) | ||
return evaluator(methodFunction.Body, methodEnv) | ||
} | ||
|
||
func createNewMethodInstanceEnvironment(method *Function, args []Object) *Environment { | ||
env := NewEnclosedEnvironment(method.Env) | ||
|
||
for i, param := range method.Parameters { | ||
if len(args) > i { | ||
env.Set(param.Value, args[i]) | ||
} | ||
} | ||
|
||
return env | ||
} |
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,24 @@ | ||
package parser | ||
|
||
import ( | ||
"github.com/sevenreup/chewa/src/ast" | ||
"github.com/sevenreup/chewa/src/token" | ||
) | ||
|
||
func (p *Parser) classStatement() ast.Expression { | ||
class := &ast.ClassStatement{Token: p.curToken} | ||
|
||
p.nextToken() | ||
|
||
class.Name = &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal} | ||
|
||
// TODO: Implement inheritance | ||
|
||
if !p.expectPeek(token.OPENING_BRACE) { | ||
return nil | ||
} | ||
|
||
class.Body = p.parseBlockStatement() | ||
|
||
return class | ||
} |
Oops, something went wrong.