Skip to content

Commit

Permalink
Fixed some issues with class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vyPal committed Nov 23, 2023
1 parent ee7c18c commit c5bbeac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 2 additions & 0 deletions example.cffc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ class Bek {

func sob() {
print this.a;
this.a = this.a + 1;
}
}

var lol: Bek = new Bek();
lol.sob();
lol.sob();
21 changes: 16 additions & 5 deletions src/parser/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,28 @@ func (p *Parser) parseClassConstructor() compiler.Method {
case "IDENT":
if token.Value == "var" {
body = append(body, p.parseVarDecl()...)
} else if token.Value == "return" {
p.Pos++ // "return"
value := p.parseExpression()
fmt.Println("Return", value)
body = append(body, &compiler.SRet{Val: value})
} else if token.Value == "if" {
body = append(body, p.parseIf())
} else if token.Value == "print" {
body = append(body, p.parsePrint())
} else if token.Value == "sleep" {
body = append(body, p.parseSleep())
} else if token.Value == "while" {
body = append(body, p.parseWhile())
} else if token.Value == "for" {
body = append(body, p.parseFor())
} else if token.Value == "func" {
body = append(body, p.parseFunctionDeclaration())
} else if token.Value == "class" {
body = append(body, p.parseClassDefinition())
} else if p.Tokens[p.Pos+1].Type == "PUNCT" && p.Tokens[p.Pos+1].Value == "(" {
body = append(body, p.parseFunctionCall())
} else if p.Tokens[p.Pos+1].Type == "PUNCT" && p.Tokens[p.Pos+1].Value == "." {
if p.Tokens[p.Pos+3].Type == "PUNCT" && p.Tokens[p.Pos+3].Value == "(" {
body = append(body, p.parseMethodCall())
} else {
body = append(body, p.parseAssignment())
}
} else {
body = append(body, p.parseAssignment())
}
Expand Down
24 changes: 17 additions & 7 deletions src/parser/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,30 @@ func (p *Parser) parseFunctionDeclaration() compiler.Stmt {
case "IDENT":
if token.Value == "var" {
body = append(body, p.parseVarDecl()...)
} else if token.Value == "return" {
p.Pos++ // "return"
value := p.parseExpression()
fmt.Println("Return", value)
body = append(body, &compiler.SRet{Val: value})
} else if token.Value == "if" {
body = append(body, p.parseIf())
} else if token.Value == "print" {
body = append(body, p.parsePrint())
} else if token.Value == "sleep" {
body = append(body, p.parseSleep())
} else if token.Value == "while" {
body = append(body, p.parseWhile())
} else if token.Value == "for" {
body = append(body, p.parseFor())
} else if token.Value == "func" {
body = append(body, p.parseFunctionDeclaration())
} else if token.Value == "class" {
body = append(body, p.parseClassDefinition())
} else if p.Tokens[p.Pos+1].Type == "PUNCT" && p.Tokens[p.Pos+1].Value == "(" {
body = append(body, p.parseFunctionCall())
} else if p.Tokens[p.Pos+1].Type == "PUNCT" && p.Tokens[p.Pos+1].Value == "." {
if p.Tokens[p.Pos+3].Type == "PUNCT" && p.Tokens[p.Pos+3].Value == "(" {
body = append(body, p.parseMethodCall())
} else {
body = append(body, p.parseAssignment())
}
} else {
fmt.Println("[W]", token.Location, "Unexpected identifier:", token.Value)
p.Pos++
body = append(body, p.parseAssignment())
}
default:
fmt.Println("[W]", token.Location, "Unexpected token:", token.Value)
Expand Down

0 comments on commit c5bbeac

Please sign in to comment.