Exemplo n.º 1
0
func fixNegatives(exp []lex.Token) []lex.Token {
	var prev lex.Token
	prev.Set(lex.Symbol, "")
	for i, a := range exp {
		if prev.Type() == lex.Symbol && a.String() == "-" &&
			(prev.String() != ")" || prev.String() == "") {
			exp[i].Set(lex.Symbol, "i")
		}
		prev = a
	}
	return exp
}
Exemplo n.º 2
0
func isBoolOp(node1, node2 lex.Token) bool {
	return node1.Type() == lex.BoolLiteral || node2.Type() == lex.BoolLiteral
}
Exemplo n.º 3
0
func (me expression) Value() (lex.Token, bool) {
	expression := []lex.Token(me)
	size := len(expression)
	if size == 0 {
		var t lex.Token
		t.SetInt(0)
		return t, true
	}
	//and/or operators
	for i := 0; i < size; i++ {
		if expression[i].Type() == lex.Symbol {
			o := expression[i]
			switch o.String() {
			case "&&", "||":
				var tree parseTree
				tree.build(expression, i)
				return tree.Value()
			}
		}
	}
	//not operator
	for i := 0; i < size; i++ {
		if expression[i].Type() == lex.Symbol {
			if expression[i].String() == "!" {
				var tree parseTree
				tree.build(expression, i)
				return tree.Value()
			}
		}
	}
	//comparison operators
	for i := 0; i < size; i++ {
		if expression[i].Type() == lex.Symbol {
			o := expression[i]
			switch o.String() {
			case "<", ">", "<=", ">=", "!=", "==":
				var tree parseTree
				tree.build(expression, i)
				return tree.Value()
			}
		}
	}
	//add and subtract
	for i := 0; i < size; i++ {
		if expression[i].Type() == lex.Symbol {
			if expression[i].String() == "+" || expression[i].String() == "-" {
				var tree parseTree
				tree.build(expression, i)
				return tree.Value()
			}
		}
	}
	//multiplication and division
	for i := 0; i < size; i++ {
		if expression[i].Type() == lex.Symbol {
			if expression[i].String() == "*" || expression[i].String() == "/" {
				var tree parseTree
				tree.build(expression, i)
				return tree.Value()
			}
		}
	}

	var t lex.Token
	if expression[0].Type() != lex.Symbol {
		t = expression[0]
	} else if expression[0].Type() == lex.Symbol { //is negative int literal
		var t lex.Token
		if expression[0].String() == "i" {
			retval, _ := strconv.Atoi(expression[1].String())
			t.SetInt(-retval)
			return t, true
		} else {
			return t, false
		}
	}
	return t, true
}
Exemplo n.º 4
0
func isStringOp(node1, node2 lex.Token) bool {
	return node1.Type() == lex.StringLiteral || node2.Type() == lex.StringLiteral
}