diff options
author | Himbeer <himbeer@disroot.org> | 2024-09-06 13:19:22 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-09-06 13:56:07 +0200 |
commit | 959599b7bf803a91595375f650245609bf7a338f (patch) | |
tree | 47aafa8d49e00e45c0cb25255c8486eeec763692 /expression.go | |
parent | 05c620c4d637b73f6b267ed57d5750587ccfd7a3 (diff) |
Add basic type system with only integers
This commit adds static typing with signed and unsigned integers of 8,
16, 32 and 64 bits.
Diffstat (limited to 'expression.go')
-rw-r--r-- | expression.go | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/expression.go b/expression.go index 76cb16b..d042ebf 100644 --- a/expression.go +++ b/expression.go @@ -5,7 +5,6 @@ package main import ( - "fmt" "io" ) @@ -63,19 +62,6 @@ func (p *paramExpr) markExpr() {} func (p *paramExpr) line() int { return p.ln } -func (p *paramExpr) String() string { - if p == nil { - return "" - } - - s := fmt.Sprintf("%s %%%s", p.typ, p.name) - if p.next != nil { - s += p.next.String() - } - - return s -} - type externFuncExpr struct { name string params *signatureExpr @@ -247,6 +233,7 @@ func (r *remAssignStmt) line() int { return r.ln } type exprExpr interface { expression markExprExpr() + cerType() cerType generate(io.Writer) (string, error) } @@ -262,6 +249,8 @@ func (e *equalityExpr) markExprExpr() {} func (e *equalityExpr) line() int { return e.ln } +func (e *equalityExpr) cerType() cerType { return e.lhs.cerType() } + type equalityRhs struct { op equalityOp value *comparisonExpr @@ -279,6 +268,8 @@ func (c *comparisonExpr) markExprExpr() {} func (c *comparisonExpr) line() int { return c.ln } +func (c *comparisonExpr) cerType() cerType { return c.lhs.cerType() } + type comparisonRhs struct { op comparisonOp value *termExpr @@ -296,6 +287,8 @@ func (t *termExpr) markExprExpr() {} func (t *termExpr) line() int { return t.ln } +func (t *termExpr) cerType() cerType { return t.lhs.cerType() } + type termRhs struct { op shiftOp value *numeralExpr @@ -313,6 +306,8 @@ func (n *numeralExpr) markExprExpr() {} func (n *numeralExpr) line() int { return n.ln } +func (n *numeralExpr) cerType() cerType { return n.lhs.cerType() } + type numeralRhs struct { op addSubOp value *factorExpr @@ -330,6 +325,8 @@ func (f *factorExpr) markExprExpr() {} func (f *factorExpr) line() int { return f.ln } +func (f *factorExpr) cerType() cerType { return f.lhs.cerType() } + type factorRhs struct { op mulDivOp value *unaryExpr @@ -347,6 +344,8 @@ func (u *unaryExpr) markExprExpr() {} func (u *unaryExpr) line() int { return u.ln } +func (u *unaryExpr) cerType() cerType { return u.value.cerType() } + type primaryExpr interface { exprExpr markPrimaryExpr() @@ -372,6 +371,8 @@ func (s *stringExpr) markLiteralExpr() {} func (s *stringExpr) line() int { return s.ln } +func (s *stringExpr) cerType() cerType { return nil } // TODO: string type + type numberExpr struct { typ string s string @@ -388,6 +389,8 @@ func (n *numberExpr) markLiteralExpr() {} func (n *numberExpr) line() int { return n.ln } +func (n *numberExpr) cerType() cerType { return resolveType(n.typ) } + type groupingExpr struct { inner exprExpr ln int @@ -401,9 +404,12 @@ func (g *groupingExpr) markPrimaryExpr() {} func (g *groupingExpr) line() int { return g.ln } +func (g *groupingExpr) cerType() cerType { return g.inner.cerType() } + type callExpr struct { funcName string args *argument + numArgs int ln int } @@ -415,6 +421,15 @@ func (c *callExpr) markPrimaryExpr() {} func (c *callExpr) line() int { return c.ln } +func (c *callExpr) cerType() cerType { + ok, _ := isDeclared(c.funcName, true) + if !ok { + return nil + } + + return funcs[c.funcName].returnType +} + type argument struct { value exprExpr next *argument @@ -432,3 +447,16 @@ func (v *varExpr) markExprExpr() {} func (v *varExpr) markPrimaryExpr() {} func (v *varExpr) line() int { return v.ln } + +func (v *varExpr) cerType() cerType { + ok, mutable := isDeclared(v.name, false) + if !ok { + return nil + } + + if mutable { + return localMuts[currentFunc][v.name].typ + } + + return localConsts[currentFunc][v.name].typ +} |