Skip to content

Commit

Permalink
Parse and generate unary expression
Browse files Browse the repository at this point in the history
  • Loading branch information
DQNEO committed Aug 29, 2019
1 parent fe21f59 commit 7fedd92
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ func tokenize() []*Token {
kind: "intliteral",
value: intliteral,
}

tokens = append(tokens, token)
fmt.Printf(" '%s'", token.value)
case ';':
case ';', '+', '-':
token := &Token{
kind: "punct",
value: string([]byte{char}),
Expand Down Expand Up @@ -95,19 +96,35 @@ func getToken() *Token {
}

type Expr struct {
kind string // "intliteral"
intval int // for intliteral
kind string // "intliteral", "unary"
intval int // for intliteral
operator string // "-", "+"
operand *Expr // for unary expr
}

func parseUnaryExpr() *Expr {
token := getToken()

intval, _ := strconv.Atoi(token.value)
expr := &Expr{
kind: "intliteral",
intval: intval,
switch token.kind {
case "intliteral":
intval, err := strconv.Atoi(token.value)
if err != nil {
panic(err)
}
return &Expr{
kind: "intliteral",
intval: intval,
}
case "punct":
operator := token.value
operand := parseUnaryExpr()
return &Expr{
kind: "unary",
operator: operator,
operand: operand,
}
default:
return nil
}
return expr
}

func parse() *Expr {
Expand All @@ -116,7 +133,21 @@ func parse() *Expr {
}

func generateExpr(expr *Expr) {
fmt.Printf(" movq $%d, %%rax\n", expr.intval)
switch expr.kind {
case "intliteral":
fmt.Printf(" movq $%d, %%rax\n", expr.intval)
case "unary":
switch expr.operator {
case "-":
fmt.Printf(" movq $-%d, %%rax\n", expr.operand.intval)
case "+":
fmt.Printf(" movq $%d, %%rax\n", expr.operand.intval)
default:
panic("generator: Unknown unary operator:" + expr.operator)
}
default:
panic("generator: Unknown expr.kind:" + expr.kind)
}
}

func generateCode(expr *Expr) {
Expand Down

0 comments on commit 7fedd92

Please sign in to comment.