Skip to content

Commit

Permalink
feat: improve printing values
Browse files Browse the repository at this point in the history
  • Loading branch information
vknabel committed Sep 17, 2021
1 parent 22bd191 commit 684c373
Show file tree
Hide file tree
Showing 23 changed files with 140 additions and 61 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- improved printing of values
- stdlib: `Variable` now moved to `rx.Variable`
- stdlib: `osEnv` and `osExit` now moved to `os.env` and `os.exit`
- compiler: `extern` fails when they can't be resolved

Expand Down
3 changes: 2 additions & 1 deletion examples/syntax.lithia
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tests
import rx

let a = 1

Expand All @@ -18,7 +19,7 @@ func just { x => Some x }
just 1
just 1

let x Variable 1
let x rx.Variable 1
x.accept "Hallo"
print x.current

Expand Down
4 changes: 2 additions & 2 deletions interpreter/binary-operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (ex *EvaluationContext) lazyLogicComparision(
boolTypeValue, err := ex.environment.LookupRuntimeValue("Bool")
boolType := RuntimeType{
name: "Bool",
modulePath: []string{"prelude"},
moduleName: "prelude",
typeValue: &boolTypeValue,
}
if err != nil {
Expand All @@ -226,7 +226,7 @@ func (ex *EvaluationContext) lazyLogicComparision(
}
trueType := RuntimeType{
name: "True",
modulePath: []string{"prelude"},
moduleName: "prelude",
typeValue: &trueTypeValue,
}
if ok, err := boolType.IncludesValue(left); !ok || err != nil {
Expand Down
2 changes: 0 additions & 2 deletions interpreter/extern-prelude.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ func (e ExternalPrelude) Lookup(name string, env *Environment) (RuntimeValue, bo
return PreludeRune(0).RuntimeType(), true
case "Function":
return PreludeFunctionType{}, true
case "Variable":
return PreludeVariableType{}, true
case "Module":
return PreludeModuleType{}, true
case "Any":
Expand Down
14 changes: 14 additions & 0 deletions interpreter/extern-rx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package interpreter

var _ ExternalDefinition = ExternalRx{}

type ExternalRx struct{}

func (e ExternalRx) Lookup(name string, env *Environment) (RuntimeValue, bool) {
switch name {
case "Variable":
return RxVariableType{}, true
default:
return nil, false
}
}
1 change: 1 addition & 0 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func NewInterpreter(importRoots ...string) *Interpreter {
}
inter.ExternalDefinitions["prelude"] = ExternalPrelude{}
inter.ExternalDefinitions["os"] = ExternalOS{}
inter.ExternalDefinitions["rx"] = ExternalRx{}
return inter
}

Expand Down
10 changes: 9 additions & 1 deletion interpreter/runtime.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package interpreter

import (
"fmt"
"reflect"
"sync"
)

type RuntimeValue interface {
RuntimeType() RuntimeType
Lookup(name string) (*LazyRuntimeValue, error)
String() string
}

var _ RuntimeValue = RuntimeType{}

type RuntimeType struct {
name string
modulePath []string
moduleName ModuleName
typeValue *RuntimeValue
}

func (t RuntimeType) String() string {
return fmt.Sprintf("%s.%s", t.moduleName, t.name)
}

func (t RuntimeType) RuntimeTypeValue() RuntimeValue {
if t.typeValue == nil {
return t.RuntimeType()
Expand Down
6 changes: 5 additions & 1 deletion interpreter/type-any-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ type PreludeAnyType struct{}
func (PreludeAnyType) RuntimeType() RuntimeType {
return RuntimeType{
name: "Any",
modulePath: []string{"prelude"},
moduleName: "prelude",
}
}

func (a PreludeAnyType) String() string {
return "Any"
}

func (a PreludeAnyType) Lookup(member string) (*LazyRuntimeValue, error) {
return nil, fmt.Errorf("any type %s has no member %s", a, member)
}
Expand Down
13 changes: 12 additions & 1 deletion interpreter/type-enum-decl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package interpreter

import "fmt"
import (
"fmt"
"strings"
)

var _ RuntimeValue = EnumDeclRuntimeValue{}

Expand All @@ -9,6 +12,14 @@ type EnumDeclRuntimeValue struct {
cases map[string]*LazyRuntimeValue
}

func (e EnumDeclRuntimeValue) String() string {
cases := make([]string, 0, len(e.cases))
for key := range e.cases {
cases = append(cases, key)
}
return fmt.Sprintf("enum %s { %s }", e.name, strings.Join(cases, ", "))
}

func (EnumDeclRuntimeValue) RuntimeType() RuntimeType {
return PreludeAnyType{}.RuntimeType()
}
Expand Down
10 changes: 8 additions & 2 deletions interpreter/type-module-type.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package interpreter

import "fmt"
import (
"fmt"
)

var _ RuntimeValue = PreludeModuleType{}

type PreludeModuleType struct{}

func (PreludeModuleType) String() string {
return "Module"
}

func (PreludeModuleType) RuntimeType() RuntimeType {
return RuntimeType{
name: "Module",
modulePath: []string{"prelude"},
moduleName: "prelude",
}
}

Expand Down
4 changes: 4 additions & 0 deletions interpreter/type-module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type RuntimeModule struct {
module *Module
}

func (m RuntimeModule) String() string {
return fmt.Sprintf("(import %s)", m.module.name)
}

func (RuntimeModule) RuntimeType() RuntimeType {
return PreludeModuleType{}.RuntimeType()
}
Expand Down
6 changes: 5 additions & 1 deletion interpreter/type-prelude-float.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ var _ RuntimeValue = PreludeFloat(0)

type PreludeFloat float64

func (f PreludeFloat) String() string {
return fmt.Sprintf("%f", f)
}

func (PreludeFloat) RuntimeType() RuntimeType {
return RuntimeType{
name: "Float",
modulePath: []string{"prelude"},
moduleName: "prelude",
}
}

Expand Down
6 changes: 5 additions & 1 deletion interpreter/type-prelude-function-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ var _ RuntimeValue = PreludeFunctionType{}

type PreludeFunctionType struct{}

func (f PreludeFunctionType) String() string {
return "Function"
}

func (PreludeFunctionType) RuntimeType() RuntimeType {
return RuntimeType{
name: "Function",
modulePath: []string{"prelude"},
moduleName: "prelude",
}
}

Expand Down
6 changes: 5 additions & 1 deletion interpreter/type-prelude-int.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ var _ RuntimeValue = PreludeInt(0)

type PreludeInt int64

func (i PreludeInt) String() string {
return fmt.Sprintf("%d", i)
}

func (PreludeInt) RuntimeType() RuntimeType {
return RuntimeType{
name: "Int",
modulePath: []string{"prelude"},
moduleName: "prelude",
}
}

Expand Down
6 changes: 5 additions & 1 deletion interpreter/type-prelude-rune.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ var _ RuntimeValue = PreludeRune('l')

type PreludeRune rune

func (r PreludeRune) String() string {
return string(r)
}

func (PreludeRune) RuntimeType() RuntimeType {
return RuntimeType{
name: "Rune",
modulePath: []string{"prelude"},
moduleName: "prelude",
}
}

Expand Down
6 changes: 5 additions & 1 deletion interpreter/type-prelude-string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ var _ RuntimeValue = PreludeString("")

type PreludeString string

func (s PreludeString) String() string {
return string(s)
}

func (PreludeString) RuntimeType() RuntimeType {
return RuntimeType{
name: "String",
modulePath: []string{"prelude"},
moduleName: "prelude",
}
}

Expand Down
38 changes: 38 additions & 0 deletions interpreter/type-rx-variable-type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package interpreter

import (
"fmt"
"sync"
)

var _ RuntimeValue = RxVariableType{}
var _ Callable = &RxVariableType{}

type RxVariableType struct{}

func (v RxVariableType) String() string {
return v.RuntimeType().String()
}

func (RxVariableType) RuntimeType() RuntimeType {
return RuntimeType{
name: "Variable",
moduleName: "rx",
}
}

func (v RxVariableType) Lookup(member string) (*LazyRuntimeValue, error) {
return nil, fmt.Errorf("variable type %s has no member %s", v, member)
}

func (v RxVariableType) Call(arguments []*LazyRuntimeValue) (RuntimeValue, error) {
var _ Callable = v
if len(arguments) != 1 {
return nil, fmt.Errorf("too many arguments for variable type %s", v)
}
value, err := arguments[0].Evaluate()
if err != nil {
return nil, err
}
return &RuntimeVariable{current: value, lock: &sync.RWMutex{}}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ type RuntimeVariable struct {
current RuntimeValue
}

func (v *RuntimeVariable) String() string {
v.lock.RLock()
defer v.lock.RUnlock()
return fmt.Sprintf("(%s %s)", v.RuntimeType().name, v.current)
}

func (*RuntimeVariable) RuntimeType() RuntimeType {
return PreludeVariableType{}.RuntimeType()
return RxVariableType{}.RuntimeType()
}

func (v *RuntimeVariable) Lookup(member string) (*LazyRuntimeValue, error) {
Expand Down
38 changes: 0 additions & 38 deletions interpreter/type-variable-type.go

This file was deleted.

3 changes: 2 additions & 1 deletion stdlib/expect/fail.lithia
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module expect

import tests
import rx

let fail = Variable tests.fail
let fail = rx.Variable tests.fail
4 changes: 0 additions & 4 deletions stdlib/prelude/shim.lithia
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ extern String {
}
extern Rune
extern Function
extern Variable {
accept value
current
}
extern Module
extern Any
6 changes: 6 additions & 0 deletions stdlib/rx/variable.lithia
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module rx

extern Variable {
accept value
current
}
Loading

0 comments on commit 684c373

Please sign in to comment.