ginger/expr/expr.go

268 lines
6.5 KiB
Go
Raw Normal View History

package expr
import (
"fmt"
"strings"
"llvm.org/llvm/bindings/go/llvm"
"github.com/mediocregopher/ginger/lexer"
)
2016-07-28 23:14:33 +00:00
// TODO empty blocks?
2016-07-23 16:59:11 +00:00
// TODO empty parenthesis
2016-07-28 20:09:33 +00:00
// TODO need to figure out how to test LLVMVal stuff
2016-07-28 23:14:33 +00:00
// TODO once we're a bit more confident, make ActualFunc
// Actual represents the actual expression in question, and has certain
// properties. It is wrapped by Expr which also holds onto contextual
// information, like the token to which Actual was originally parsed from
type Actual interface {
2016-07-28 19:57:17 +00:00
// Initializes an llvm.Value and returns it.
2016-07-29 00:39:18 +00:00
LLVMVal(*Ctx, llvm.Builder) llvm.Value
}
// equaler is used to compare two expressions. The comparison should not take
// into account Token values, only the actual value being represented
type equaler interface {
equal(equaler) bool
}
// Expr contains the actual expression as well as some contextual information
// wrapping it. Most interactions will be with this and not with the Actual
// directly.
type Expr struct {
Actual Actual
// Token is a nice-to-have, nothing will break if it's not there
Token lexer.Token
val *llvm.Value
}
// LLVMVal passes its arguments to the underlying Actual instance. It caches the
// result, so if this is called multiple times the underlying one is only called
// the first time.
2016-07-29 00:39:18 +00:00
func (e Expr) LLVMVal(ctx *Ctx, builder llvm.Builder) llvm.Value {
if e.val == nil {
2016-07-29 00:39:18 +00:00
v := e.Actual.LLVMVal(ctx, builder)
e.val = &v
}
return *e.val
}
// will panic if either Expr's Actual doesn't implement equaler
func (e Expr) equal(e2 Expr) bool {
eq1, ok1 := e.Actual.(equaler)
eq2, ok2 := e2.Actual.(equaler)
if !ok1 || !ok2 {
panic(fmt.Sprintf("can't compare %T and %T", e.Actual, e2.Actual))
}
return eq1.equal(eq2)
}
////////////////////////////////////////////////////////////////////////////////
// Bool represents a true or false value
type Bool bool
// LLVMVal implements the Actual interface method
2016-07-29 00:39:18 +00:00
func (b Bool) LLVMVal(ctx *Ctx, builder llvm.Builder) llvm.Value {
return llvm.Value{}
}
func (b Bool) equal(e equaler) bool {
bb, ok := e.(Bool)
if !ok {
return false
}
return bb == b
}
////////////////////////////////////////////////////////////////////////////////
// Int represents an integer value
type Int int64
// LLVMVal implements the Actual interface method
2016-07-29 00:39:18 +00:00
func (i Int) LLVMVal(ctx *Ctx, builder llvm.Builder) llvm.Value {
v := builder.CreateAlloca(llvm.Int64Type(), "")
builder.CreateStore(llvm.ConstInt(llvm.Int64Type(), uint64(i), false), v)
return v
}
func (i Int) equal(e equaler) bool {
ii, ok := e.(Int)
if !ok {
return false
}
return ii == i
}
////////////////////////////////////////////////////////////////////////////////
// String represents a string value
type String string
// LLVMVal implements the Actual interface method
2016-07-29 00:39:18 +00:00
func (s String) LLVMVal(ctx *Ctx, builder llvm.Builder) llvm.Value {
return llvm.Value{}
}
func (s String) equal(e equaler) bool {
ss, ok := e.(String)
if !ok {
return false
}
return ss == s
}
////////////////////////////////////////////////////////////////////////////////
// Identifier represents a binding to some other value which has been given a
// name
type Identifier string
// LLVMVal implements the Actual interface method
2016-07-29 00:39:18 +00:00
func (id Identifier) LLVMVal(ctx *Ctx, builder llvm.Builder) llvm.Value {
return llvm.Value{}
}
func (id Identifier) equal(e equaler) bool {
idid, ok := e.(Identifier)
if !ok {
return false
}
return idid == id
}
////////////////////////////////////////////////////////////////////////////////
2016-07-24 21:04:06 +00:00
// Macro is an identifier for a macro which can be used to transform
// expressions. The tokens for macros start with a '%', but the Macro identifier
// itself has that stripped off
type Macro string
// String returns the Macro with a '%' prepended to it
func (m Macro) String() string {
return "%" + string(m)
}
// LLVMVal implements the Actual interface method
2016-07-29 00:39:18 +00:00
func (m Macro) LLVMVal(ctx *Ctx, builder llvm.Builder) llvm.Value {
panic("Macros have no inherent LLVMVal")
}
func (m Macro) equal(e equaler) bool {
2016-07-24 21:04:06 +00:00
mm, ok := e.(Macro)
if !ok {
return false
}
return m == mm
}
////////////////////////////////////////////////////////////////////////////////
// Tuple represents a fixed set of expressions which are interacted with as if
// they were a single value
type Tuple []Expr
func (tup Tuple) String() string {
strs := make([]string, len(tup))
for i := range tup {
strs[i] = fmt.Sprint(tup[i].Actual)
}
return "(" + strings.Join(strs, ", ") + ")"
}
// LLVMVal implements the Actual interface method
2016-07-29 00:39:18 +00:00
func (tup Tuple) LLVMVal(ctx *Ctx, builder llvm.Builder) llvm.Value {
return llvm.Value{}
}
func (tup Tuple) equal(e equaler) bool {
tuptup, ok := e.(Tuple)
if !ok || len(tuptup) != len(tup) {
return false
}
for i := range tup {
if !tup[i].equal(tuptup[i]) {
return false
}
}
return true
}
////////////////////////////////////////////////////////////////////////////////
// Statement represents an actual action which will be taken. The input value is
// used as the input to the pipe, and the output of the pipe is the output of
// the statement
2016-07-22 21:53:27 +00:00
type Statement struct {
2016-07-28 19:57:17 +00:00
In Expr
To Expr
2016-07-22 21:53:27 +00:00
}
func (s Statement) String() string {
2016-07-28 19:57:17 +00:00
return fmt.Sprintf("(%v > %s)", s.In.Actual, s.To.Actual)
2016-07-22 21:53:27 +00:00
}
// LLVMVal implements the Actual interface method
2016-07-29 00:39:18 +00:00
func (s Statement) LLVMVal(ctx *Ctx, builder llvm.Builder) llvm.Value {
2016-07-28 19:57:17 +00:00
m, ok := s.To.Actual.(Macro)
if !ok {
// TODO proper error
panic("statement To is not a macro")
}
2016-07-29 00:39:18 +00:00
fn := ctx.GetMacro(m)
if fn == nil {
2016-07-28 19:57:17 +00:00
// TODO proper error
panic(fmt.Sprintf("unknown macro %q", m))
}
newe, err := fn(s.In)
if err != nil {
// TODO proper error
panic(err)
}
2016-07-29 00:39:18 +00:00
return newe.LLVMVal(ctx, builder)
}
func (s Statement) equal(e equaler) bool {
ss, ok := e.(Statement)
return ok && s.In.equal(ss.In) && s.To.equal(ss.To)
}
2016-07-22 21:53:27 +00:00
////////////////////////////////////////////////////////////////////////////////
// Block represents a set of statements which share a scope, i.e. If one
// statement binds a variable the rest of the statements in the block can use
// that variable, including sub-blocks within this one.
type Block []Statement
2016-07-22 21:53:27 +00:00
func (b Block) String() string {
strs := make([]string, len(b))
for i := range b {
strs[i] = b[i].String()
2016-07-22 21:53:27 +00:00
}
return fmt.Sprintf("{ %s }", strings.Join(strs, " "))
}
// LLVMVal implements the Actual interface method
2016-07-29 00:39:18 +00:00
func (b Block) LLVMVal(ctx *Ctx, builder llvm.Builder) llvm.Value {
return llvm.Value{}
}
func (b Block) equal(e equaler) bool {
2016-07-22 21:53:27 +00:00
bb, ok := e.(Block)
if !ok {
return false
}
for i := range b {
if !b[i].equal(bb[i]) {
2016-07-22 21:53:27 +00:00
return false
}
}
return true
}