Skip to content

Commit

Permalink
add class definition
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Aug 19, 2024
1 parent 0e87313 commit 10dd438
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
4 changes: 4 additions & 0 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ func (interp *interpreter) executeStatement(s parser.Statement) {
case *parser.Return:
result := interp.evaluate(s.Result)
panic(returnResult{result, s.Position()})
case *parser.ClassDefinition:
//print(s.Name)
//interp.executeBlock(s.Body)

default:
// Parser should never get us here
panic(fmt.Sprintf("unexpected statement type %T", s))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,29 @@ import (
"github.com/DavinciScript/Davi/lexer"
"github.com/DavinciScript/Davi/parser"
"github.com/hokaccha/go-prettyjson"
"io/ioutil"
"os"
"strings"
)

func main() {

fmt.Println("Running davi.go")
filename := os.Args[1]

input := []byte(`
input, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Printf("Error reading file. Please check the file path and try again.\n")
os.Exit(1)
}

// Replace <?davi with empty string
input = bytes.Replace(input, []byte("<?davi"), []byte(""), 1)

// Replace ?> with empty string
input = bytes.Replace(input, []byte("?>"), []byte(""), 1)

// This is a comment
//$firstMessage = "Hello World";
//$secondMessage = "Hello DavinciScript";
//
//function person($name, $age) {
//
//}
//
//echo "$firstMessage";
//echo ($secondMessage, $firstMessage);

echo("qko");

`)
// Trim
input = bytes.TrimSpace(input)

prog, err := parser.ParseProgram(input)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,23 @@ func (e *Map) String() string {
return fmt.Sprintf("{%s}", strings.Join(items, ", "))
}

type ClassDefinition struct {
pos Position
Name string
Body Block
}

func (e *ClassDefinition) statementNode() {}
func (e *ClassDefinition) Position() Position { return e.pos }
func (e *ClassDefinition) String() string {
bodyStr := ""
if len(e.Body) != 0 {
bodyStr = "\n" + indent(e.Body.String()) + "\n"
}
print("class %s {%s}", e.Name, bodyStr)
return fmt.Sprintf("class %s {%s}", e.Name, bodyStr)
}

type FunctionExpression struct {
pos Position
Parameters []string
Expand Down
16 changes: 16 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func (p *parser) statement() Statement {
return p.return_()
case FUNCTION:
return p.function_()
case CLASS:
return p.class_()
}
pos := p.pos
expr := p.expression()
Expand Down Expand Up @@ -167,6 +169,20 @@ func (p *parser) return_() Statement {
return &Return{pos, result}
}

// class = CLASS NAME block
func (p *parser) class_() Statement {

p.next()
pos := p.pos
name := p.val
p.expect(NAME)
//p.expect(LBRACE)
body := p.block()
//p.expect(RBRACE)

return &ClassDefinition{pos, name, body}
}

// function = FUNCTION NAME params block |
//
// FUNCTION params block
Expand Down

0 comments on commit 10dd438

Please sign in to comment.