diff --git a/lang/check/check.go b/lang/check/check.go index 6658b338..fb50fe90 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -19,6 +19,8 @@ var ( TypeExprIdealNumber = a.NewTypeExpr(0, t.IDIdeal, nil, nil, nil) ) +func numeric(n *a.TypeExpr) bool { return n == TypeExprIdealNumber || n.IsNumType() } + // TypeMap maps from variable names (as token IDs) to types. type TypeMap map[t.ID]*a.TypeExpr @@ -158,4 +160,12 @@ var numTypeRanges = [256][2]*big.Int{ var ( zero = big.NewInt(0) one = big.NewInt(1) + ffff = big.NewInt(0xFFFF) ) + +func btoi(b bool) *big.Int { + if b { + return one + } + return zero +} diff --git a/lang/check/check_test.go b/lang/check/check_test.go index b9b88ed8..c170d3cc 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -6,6 +6,7 @@ package check import ( "bytes" "fmt" + "math/big" "reflect" "sort" "strings" @@ -43,9 +44,9 @@ func compareToPuffsfmt(idMap *token.IDMap, tokens []token.Token, src string) err } func TestCheck(t *testing.T) { - const filename = "foo.puffs" + const filename = "test.puffs" src := strings.TrimSpace(` - func bar()() { + func foo()() { var x u8 var y i32 = +2 var z u64[:123] @@ -53,6 +54,7 @@ func TestCheck(t *testing.T) { var b bool x = 0 + x = 1 + 2 y = -y b = not true @@ -85,18 +87,18 @@ func TestCheck(t *testing.T) { if len(funcs) != 1 { t.Fatalf("Funcs: got %d elements, want 1", len(funcs)) } - bar := Func{} + foo := Func{} for _, f := range funcs { - bar = f + foo = f break } - if got, want := idMap.ByID(bar.QID[1]), "bar"; got != want { + if got, want := idMap.ByID(foo.QID[1]), "foo"; got != want { t.Fatalf("Funcs[0] name: got %q, want %q", got, want) } got := [][2]string(nil) - for id, typ := range bar.LocalVars { + for id, typ := range foo.LocalVars { got = append(got, [2]string{ idMap.ByID(id), typ.String(idMap), @@ -152,3 +154,88 @@ func walk(n *ast.Node, f func(*ast.Node) error) error { } return nil } + +func TestConstValues(t *testing.T) { + const filename = "test.puffs" + testCases := map[string]int64{ + "var i i32 = 42": 42, + + "var i i32 = +7": +7, + "var i i32 = -7": -7, + + "var b bool = false": 0, + "var b bool = not false": 1, + "var b bool = not not false": 0, + + "var i i32 = 10 + 3": 13, + "var i i32 = 10 - 3": 7, + "var i i32 = 10 * 3": 30, + "var i i32 = 10 / 3": 3, + "var i i32 = 10 << 3": 80, + "var i i32 = 10 >> 3": 1, + "var i i32 = 10 & 3": 2, + "var i i32 = 10 &^ 3": 8, + "var i i32 = 10 | 3": 11, + "var i i32 = 10 ^ 3": 9, + + "var b bool = 10 != 3": 1, + "var b bool = 10 < 3": 0, + "var b bool = 10 <= 3": 0, + "var b bool = 10 == 3": 0, + "var b bool = 10 >= 3": 1, + "var b bool = 10 > 3": 1, + + "var b bool = false and true": 0, + "var b bool = false or true": 1, + } + + idMap := &token.IDMap{} + for s, wantInt64 := range testCases { + src := "func foo()() {\n\t" + s + "\n}\n" + + tokens, _, err := token.Tokenize(idMap, filename, []byte(src)) + if err != nil { + t.Errorf("%q: Tokenize: %v", s, err) + continue + } + + file, err := parse.Parse(idMap, filename, tokens) + if err != nil { + t.Errorf("%q: Parse: %v", s, err) + continue + } + + c, err := Check(idMap, file) + if err != nil { + t.Errorf("%q: Check: %v", s, err) + continue + } + + funcs := c.Funcs() + if len(funcs) != 1 { + t.Errorf("%q: Funcs: got %d elements, want 1", s, len(funcs)) + continue + } + foo := Func{} + for _, f := range funcs { + foo = f + break + } + body := foo.Func.Body() + if len(body) != 1 { + t.Errorf("%q: Body: got %d elements, want 1", s, len(body)) + continue + } + if body[0].Kind() != ast.KVar { + t.Errorf("%q: Body[0]: got %s, want %s", s, body[0].Kind(), ast.KVar) + continue + } + + got := body[0].Var().Value().ConstValue() + want := big.NewInt(wantInt64) + if got == nil || want == nil || got.Cmp(want) != 0 { + t.Errorf("%q: got %v, want %v", s, got, want) + continue + } + } +} diff --git a/lang/check/type.go b/lang/check/type.go index 20dd533c..fc6a484e 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -221,13 +221,9 @@ func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error { switch n.ID0() { case t.IDXUnaryPlus, t.IDXUnaryMinus: - if rTyp != TypeExprIdealNumber && !rTyp.IsNumType() { - op := '+' - if n.ID0() == t.IDXUnaryMinus { - op = '-' - } - return fmt.Errorf(`check: unary "%c": %q, of type %q, does not have a numeric type`, - op, rhs.String(c.idMap), rTyp.String(c.idMap)) + if !numeric(rTyp) { + return fmt.Errorf("check: unary %q: %q, of type %q, does not have a numeric type", + c.idMap.ByID(n.ID0().AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap)) } if cv := rhs.ConstValue(); cv != nil { if n.ID0() == t.IDXUnaryMinus { @@ -240,16 +236,11 @@ func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error { case t.IDXUnaryNot: if !rTyp.Eq(TypeExprBoolean) { - return fmt.Errorf(`check: unary "not": %q, of type %q, does not have a boolean type`, - rhs.String(c.idMap), rTyp.String(c.idMap)) + return fmt.Errorf("check: unary %q: %q, of type %q, does not have a boolean type", + c.idMap.ByID(n.ID0().AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap)) } if cv := rhs.ConstValue(); cv != nil { - if cv.Cmp(zero) == 0 { - cv = one - } else { - cv = zero - } - n.SetConstValue(cv) + n.SetConstValue(btoi(cv.Cmp(zero) == 0)) } n.SetMType(TypeExprBoolean) return nil @@ -262,13 +253,14 @@ func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error { if err := c.checkExpr(lhs); err != nil { return err } - if n.ID0() == t.IDXBinaryAs { + lTyp := lhs.MType() + op := n.ID0() + if op == t.IDXBinaryAs { rhs := n.RHS().TypeExpr() if err := c.checkTypeExpr(rhs); err != nil { return err } - lTyp := lhs.MType() - if (lTyp == TypeExprIdealNumber || lTyp.IsNumType()) && rhs.IsNumType() { + if numeric(lTyp) && rhs.IsNumType() { n.SetMType(rhs) return nil } @@ -279,9 +271,104 @@ func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error { if err := c.checkExpr(rhs); err != nil { return err } - // TODO: check lhs and rhs have compatible types, then call n.SetMType. - // TODO: other checks. - return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprBinaryOp", n.ID0().Key()) + rTyp := rhs.MType() + + switch op { + default: + if !numeric(lTyp) { + return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", + c.idMap.ByID(op.AmbiguousForm()), lhs.String(c.idMap), lTyp.String(c.idMap)) + } + if !numeric(rTyp) { + return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", + c.idMap.ByID(op.AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap)) + } + case t.IDXBinaryNotEq, t.IDXBinaryEqEq: + // No-op. + case t.IDXBinaryAnd, t.IDXBinaryOr: + if lTyp != TypeExprBoolean { + return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type", + c.idMap.ByID(op.AmbiguousForm()), lhs.String(c.idMap), lTyp.String(c.idMap)) + } + if rTyp != TypeExprBoolean { + return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type", + c.idMap.ByID(op.AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap)) + } + } + if op != t.IDXBinaryShiftL && op != t.IDXBinaryShiftR && !lTyp.Eq(rTyp) { + return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, does not have matching types", + c.idMap.ByID(op.AmbiguousForm()), + lhs.String(c.idMap), rhs.String(c.idMap), + lTyp.String(c.idMap), rTyp.String(c.idMap), + ) + } + + if l, r := lhs.ConstValue(), rhs.ConstValue(); l != nil && r != nil { + if err := c.setConstValueBinaryOp(n, l, r); err != nil { + return err + } + } + if comparisonOps[0xFF&op.Key()] { + n.SetMType(TypeExprBoolean) + } else { + n.SetMType(lTyp) + } + return nil +} + +func (c *typeChecker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error { + switch n.ID0() { + case t.IDXBinaryPlus: + n.SetConstValue(big.NewInt(0).Add(l, r)) + case t.IDXBinaryMinus: + n.SetConstValue(big.NewInt(0).Sub(l, r)) + case t.IDXBinaryStar: + n.SetConstValue(big.NewInt(0).Mul(l, r)) + case t.IDXBinarySlash: + if r.Cmp(zero) == 0 { + return fmt.Errorf("check: division by zero in const expression %q", n.String(c.idMap)) + } + // TODO: decide on Euclidean division vs other definitions. See "go doc + // math/big int.divmod" for details. + n.SetConstValue(big.NewInt(0).Div(l, r)) + case t.IDXBinaryShiftL: + if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 { + return fmt.Errorf("check: shift %q out of range in const expression %q", + n.RHS().Expr().String(c.idMap), n.String(c.idMap)) + } + n.SetConstValue(big.NewInt(0).Lsh(l, uint(r.Uint64()))) + case t.IDXBinaryShiftR: + if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 { + return fmt.Errorf("check: shift %q out of range in const expression %q", + n.RHS().Expr().String(c.idMap), n.String(c.idMap)) + } + n.SetConstValue(big.NewInt(0).Rsh(l, uint(r.Uint64()))) + case t.IDXBinaryAmp: + n.SetConstValue(big.NewInt(0).And(l, r)) + case t.IDXBinaryAmpHat: + n.SetConstValue(big.NewInt(0).AndNot(l, r)) + case t.IDXBinaryPipe: + n.SetConstValue(big.NewInt(0).Or(l, r)) + case t.IDXBinaryHat: + n.SetConstValue(big.NewInt(0).Xor(l, r)) + case t.IDXBinaryNotEq: + n.SetConstValue(btoi(l.Cmp(r) != 0)) + case t.IDXBinaryLessThan: + n.SetConstValue(btoi(l.Cmp(r) < 0)) + case t.IDXBinaryLessEq: + n.SetConstValue(btoi(l.Cmp(r) <= 0)) + case t.IDXBinaryEqEq: + n.SetConstValue(btoi(l.Cmp(r) == 0)) + case t.IDXBinaryGreaterEq: + n.SetConstValue(btoi(l.Cmp(r) >= 0)) + case t.IDXBinaryGreaterThan: + n.SetConstValue(btoi(l.Cmp(r) > 0)) + case t.IDXBinaryAnd: + n.SetConstValue(btoi((l.Cmp(zero) != 0) && (r.Cmp(zero) != 0))) + case t.IDXBinaryOr: + n.SetConstValue(btoi((l.Cmp(zero) != 0) || (r.Cmp(zero) != 0))) + } + return nil } func (c *typeChecker) checkExprAssociativeOp(n *a.Expr) error { @@ -332,3 +419,12 @@ func (c *typeChecker) checkTypeExpr(n *a.TypeExpr) error { } return nil } + +var comparisonOps = [256]bool{ + t.IDXBinaryNotEq >> t.KeyShift: true, + t.IDXBinaryLessThan >> t.KeyShift: true, + t.IDXBinaryLessEq >> t.KeyShift: true, + t.IDXBinaryEqEq >> t.KeyShift: true, + t.IDXBinaryGreaterEq >> t.KeyShift: true, + t.IDXBinaryGreaterThan >> t.KeyShift: true, +} diff --git a/lang/token/list.go b/lang/token/list.go index c1837dbc..821d43b3 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -46,6 +46,7 @@ const ( // ID combines a Key and Flags. type ID uint32 +func (x ID) AmbiguousForm() ID { return ambiguousForms[0xFF&(x>>KeyShift)] } func (x ID) UnaryForm() ID { return unaryForms[0xFF&(x>>KeyShift)] } func (x ID) BinaryForm() ID { return binaryForms[0xFF&(x>>KeyShift)] } func (x ID) AssociativeForm() ID { return associativeForms[0xFF&(x>>KeyShift)] } @@ -354,6 +355,7 @@ var squiggles = [256]ID{ // 1<> KeyShift: IDPlus, + IDXUnaryMinus >> KeyShift: IDMinus, + IDXUnaryNot >> KeyShift: IDNot, + + IDXBinaryPlus >> KeyShift: IDPlus, + IDXBinaryMinus >> KeyShift: IDMinus, + IDXBinaryStar >> KeyShift: IDStar, + IDXBinarySlash >> KeyShift: IDSlash, + IDXBinaryShiftL >> KeyShift: IDShiftL, + IDXBinaryShiftR >> KeyShift: IDShiftR, + IDXBinaryAmp >> KeyShift: IDAmp, + IDXBinaryAmpHat >> KeyShift: IDAmpHat, + IDXBinaryPipe >> KeyShift: IDPipe, + IDXBinaryHat >> KeyShift: IDHat, + IDXBinaryNotEq >> KeyShift: IDNotEq, + IDXBinaryLessThan >> KeyShift: IDLessThan, + IDXBinaryLessEq >> KeyShift: IDLessEq, + IDXBinaryEqEq >> KeyShift: IDEqEq, + IDXBinaryGreaterEq >> KeyShift: IDGreaterEq, + IDXBinaryGreaterThan >> KeyShift: IDGreaterThan, + IDXBinaryAnd >> KeyShift: IDAnd, + IDXBinaryOr >> KeyShift: IDOr, + IDXBinaryAs >> KeyShift: IDAs, + + IDXAssociativePlus >> KeyShift: IDPlus, + IDXAssociativeStar >> KeyShift: IDStar, + IDXAssociativeAmp >> KeyShift: IDAmp, + IDXAssociativePipe >> KeyShift: IDPipe, + IDXAssociativeHat >> KeyShift: IDHat, + IDXAssociativeAnd >> KeyShift: IDAnd, + IDXAssociativeOr >> KeyShift: IDOr, +} + var unaryForms = [256]ID{ IDPlus >> KeyShift: IDXUnaryPlus, IDMinus >> KeyShift: IDXUnaryMinus,