Implement ifeq and ifneq
diff --git a/eval.go b/eval.go
index 4d4c07f..ef4eda0 100644
--- a/eval.go
+++ b/eval.go
@@ -222,20 +222,25 @@
}
func (ev *Evaluator) evalIf(ast *IfAST) {
- var stmts []AST
+ var isTrue bool
switch ast.op {
case "ifdef", "ifndef":
value, _ := ev.getVar(ev.evalExpr(ast.lhs))
- if (value != "") == (ast.op == "ifdef") {
- stmts = ast.trueStmts
- } else {
- stmts = ast.falseStmts
- }
+ isTrue = (value != "") == (ast.op == "ifdef")
case "ifeq", "ifneq":
- panic("TODO")
+ lhs := ev.evalExpr(ast.lhs)
+ rhs := ev.evalExpr(ast.rhs)
+ isTrue = (lhs == rhs) == (ast.op == "ifeq")
default:
panic(fmt.Sprintf("unknown if statement: %q", ast.op))
}
+
+ var stmts []AST
+ if isTrue {
+ stmts = ast.trueStmts
+ } else {
+ stmts = ast.falseStmts
+ }
for _, stmt := range stmts {
ev.eval(stmt)
}
diff --git a/parser.go b/parser.go
index 513305b..829379b 100644
--- a/parser.go
+++ b/parser.go
@@ -157,10 +157,61 @@
return ast
}
+func (p *parser) parseEq(s string) (string, string, bool) {
+ if len(s) == 0 || s[0] != '(' {
+ return "", "", false
+ }
+
+ // TODO: Double-quotes will not be handled properly.
+ i := 0
+ paren_cnt := 0
+ in_rhs := false
+ var lhs []byte
+ var rhs []byte
+ for {
+ i++
+ if i == len(s) {
+ return "", "", false
+ }
+ ch := s[i]
+ if ch == '(' {
+ paren_cnt++
+ } else if ch == ')' {
+ paren_cnt--
+ if paren_cnt < 0 {
+ if in_rhs {
+ break
+ } else {
+ return "", "", false
+ }
+ }
+ } else if ch == ',' {
+ if in_rhs {
+ return "", "", false
+ } else {
+ in_rhs = true
+ continue
+ }
+ }
+ if in_rhs {
+ rhs = append(rhs, ch)
+ } else {
+ lhs = append(lhs, ch)
+ }
+ }
+ return string(lhs), string(rhs), true
+}
+
func (p *parser) parseIfeq(line string, oplen int) AST {
+ lhs, rhs, ok := p.parseEq(strings.TrimSpace(line[oplen+1:]))
+ if !ok {
+ Error(p.filename, p.lineno, `*** invalid syntax in conditional.`)
+ }
+
ast := &IfAST{
op: line[:oplen],
- lhs: strings.TrimSpace(line[oplen+1:]),
+ lhs: lhs,
+ rhs: rhs,
}
ast.filename = p.filename
ast.lineno = p.lineno
diff --git a/test/cond_syntax.mk b/test/cond_syntax.mk
index e5ba2eb..93d7d37 100644
--- a/test/cond_syntax.mk
+++ b/test/cond_syntax.mk
@@ -56,29 +56,27 @@
RESULT += FAIL
endif
-# TODO: Support ifeq and ifneq
+ifeq ($(VAR),var)
+RESULT += PASS
+else
+RESULT += FAIL
+endif
+ifneq ($(VAR),var)
+RESULT += FAIL
+else
+RESULT += PASS
+endif
-# ifeq ($(VAR),var)
-# RESULT += PASS
-# else
-# RESULT += FAIL
-# endif
-# ifneq ($(VAR),var)
-# RESULT += FAIL
-# else
-# RESULT += PASS
-# endif
-
-# ifeq ($(UNDEFINED),)
-# RESULT += PASS
-# else
-# RESULT += FAIL
-# endif
-# ifeq (,$(UNDEFINED))
-# RESULT += PASS
-# else
-# RESULT += FAIL
-# endif
+ifeq ($(UNDEFINED),)
+RESULT += PASS
+else
+RESULT += FAIL
+endif
+ifeq (,$(UNDEFINED))
+RESULT += PASS
+else
+RESULT += FAIL
+endif
# TODO: Support?
# ifeq "$(VAR)" "var"