Skip to content

Commit

Permalink
Add parsing for classic for stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nmsobri committed Jun 17, 2024
1 parent 04fc3e1 commit c2f0b96
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ First stage, the code will be evaluated at run time ( interpreted )
Second stage would be compile it to custom byte code and then run it
Hopefully it will run the following code

```
```go
const fmt = import("fmt")

struct User {
Expand All @@ -27,16 +27,24 @@ fn say(greet) {

fn main() {
let arr = [1,2,3,4,5]
let foo = arr[0]
let map = {name: "sobri", location: "penang"}
let name = map["name"]

let user = User{name:"Sobri", age:82, job:"Programmer"}
let user = User{name:"Sobri", age:99, job:"Programmer"}
user.setAge(99)
user.info()

say("hello world")

let i = 1 * 2 + 1

if 10 > 2 {
fmt.Println("bigger than 2")
} else {
fmt.Println("smaller than 2")
}

if i < 3 {
fmt.Print("lower than 3\n")
} else if i < 5 {
Expand All @@ -47,7 +55,12 @@ fn main() {

for i > 0 {
fmt.Print(i)
i--
1 + i--
1 + --i
---3
--1 + 2
++5--
--6--
}

for let j = 0; j < 5; j++ {
Expand Down
19 changes: 16 additions & 3 deletions ast/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,28 @@ func (np NodeProgram) String() string {
}

// #######################################################
// ##################### Node For Stmt ###################
// ################## Node Modern For Stmt ###############
// #######################################################
type NodeForStmt struct {
type NodeModernForStmt struct {
Token token.Token
Condition Expr
Body BlockStmt
}

func (nf NodeForStmt) stmt() {}
func (nf NodeModernForStmt) stmt() {}

// #######################################################
// ################# Node Classic For Stmt ###############
// #######################################################
type NodeClassicForStmt struct {
Token token.Token
Condition Expr
PreExpr Stmt
PostExpr Expr
Body BlockStmt
}

func (nf NodeClassicForStmt) stmt() {}

// #######################################################
// #################### Node Const Stmt ##################
Expand Down
8 changes: 4 additions & 4 deletions doc/main.kat
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
let map = {name: "sobri", location: "penang"}
let name = map["name"]

let user = User{name:"Sobri", age:82, job:"Programmer"}
let user = User{name:"Sobri", age:99, job:"Programmer"}
user.setAge(99)
user.info()

Expand Down Expand Up @@ -56,7 +56,7 @@ fn main() {
--6--
}

// for let j = 0; j < 5; j++ {
// fmt.Print(j)
// }
for let j = 0; j < 5; j++ {
fmt.Print(j)
}
}
45 changes: 37 additions & 8 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,15 +544,11 @@ func (p *Parser) parseBlockStmt() ast.BlockStmt {
}

func (p *Parser) parseForStmt() ast.Stmt {
currentToken := p.CurrentToken()
condition := p.ParseExpression(token.Precedence.LOWEST + 1)
body := p.parseBlockStmt()

return ast.NodeForStmt{
Token: currentToken,
Condition: condition,
Body: body,
if p.PeekToken().Type == token.LET {
return p.parseClassicForStmt()
}

return p.parseModernForStmt()
}

func (p *Parser) parsePostfixExpr(left ast.Expr) ast.Expr {
Expand All @@ -575,3 +571,36 @@ func (p *Parser) ParseStatement() ast.Stmt {
func (p *Parser) ParseExpressionStatement() ast.Stmt {
return ast.NodeExprStmt{Expression: p.ParseExpression(token.Precedence.LOWEST)}
}

func (p *Parser) parseModernForStmt() ast.Stmt {
currentToken := p.CurrentToken()
condition := p.ParseExpression(token.Precedence.LOWEST + 1)
body := p.parseBlockStmt()

return ast.NodeModernForStmt{
Token: currentToken,
Condition: condition,
Body: body,
}
}

func (p *Parser) parseClassicForStmt() ast.Stmt {
currentToken := p.CurrentToken()
preExpr := p.ParseStatement()
p.ExpectToken(token.SEMICOLON)

condition := p.ParseExpression(token.Precedence.LOWEST)
p.ExpectToken(token.SEMICOLON)

postExpr := p.ParseExpression(token.Precedence.LOWEST + 1)

body := p.parseBlockStmt()

return ast.NodeClassicForStmt{
Token: currentToken,
Condition: condition,
PreExpr: preExpr,
PostExpr: postExpr,
Body: body,
}
}

0 comments on commit c2f0b96

Please sign in to comment.