Skip to content

Commit

Permalink
[interpreter,spec] Rename Value to MTValue.
Browse files Browse the repository at this point in the history
In the past, collision between the Myst-level type `Value` and Crystal's native `Value` class has been cumbersome (requiring namespacing as `Myst::Value`). Renaming to `MTValue` clears that confusion, and makes it more obvious where we're talking about Myst values.
  • Loading branch information
faultyserver committed Feb 24, 2018
1 parent 56d3726 commit 2efe944
Show file tree
Hide file tree
Showing 32 changed files with 155 additions and 155 deletions.
2 changes: 1 addition & 1 deletion spec/interpreter/invocation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private def it_invokes(prelude, call, expected)
# clarity in the tests, the stack is cleared of any existing values before
# making any assertions.
itr.stack.clear
it_interprets(call, [expected] of Myst::Value, itr)
it_interprets(call, [expected] of MTValue, itr)
end

describe "Interpreter - Invocation" do
Expand Down
4 changes: 2 additions & 2 deletions spec/interpreter/nodes/interpolation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ describe "Interpreter - Interpolation" do
it_interprets %q(<[1]>), [TList.new([val(1)])]
it_interprets %q(<[1, 2, 3]>), [TList.new([val(1), val(2), val(3)])]
it_interprets %q(<[nil, :hi]>), [TList.new([val(nil), val(:hi)])]
it_interprets %q(<[[1, 2], [3, 4]]>), [TList.new([TList.new([val(1), val(2)]), TList.new([val(3), val(4)])] of Myst::Value)]
it_interprets %q(<[[1, 2], [3, 4]]>), [TList.new([TList.new([val(1), val(2)]), TList.new([val(3), val(4)])] of MTValue)]

it_interprets %q(<{a: 1 }>), [TMap.new({ val(:a) => val(1) })]
it_interprets %q(<{<1>: "int", <nil>: :nil }>), [TMap.new({ val(1) => val("int"), val(nil) => val(:nil) })]
it_interprets %q(<{<{a: 1}>: {b: 2}}>), [TMap.new({ TMap.new({ val(:a) => val(1) }) => TMap.new({ val(:b) => val(2) }) } of Myst::Value => Myst::Value)]
it_interprets %q(<{<{a: 1}>: {b: 2}}>), [TMap.new({ TMap.new({ val(:a) => val(1) }) => TMap.new({ val(:b) => val(2) }) } of MTValue => MTValue)]

it_interprets %q(<(true && true)>), [val(true)]
it_interprets %q(<(false && true)>), [val(false)]
Expand Down
4 changes: 2 additions & 2 deletions spec/interpreter/nodes/literals_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe "Interpreter - Literals" do
it_interprets %q([1]), [TList.new([val(1)])]
it_interprets %q([1, 2, 3]), [TList.new([val(1), val(2), val(3)])]
it_interprets %q([nil, :hi]), [TList.new([val(nil), val(:hi)])]
it_interprets %q([[1, 2], [3, 4]]), [TList.new([TList.new([val(1), val(2)]), TList.new([val(3), val(4)])] of Myst::Value)]
it_interprets %q([[1, 2], [3, 4]]), [TList.new([TList.new([val(1), val(2)]), TList.new([val(3), val(4)])] of MTValue)]

it_interprets %q({a: 1}), [TMap.new({ val(:a) => val(1) })]
it_interprets %q({<1>: "int", <nil>: :nil}), [TMap.new({ val(1) => val("int"), val(nil) => val(:nil) })]
it_interprets %q({<{a: 1}>: {b: 2}}), [TMap.new({ TMap.new({ val(:a) => val(1) }) => TMap.new({ val(:b) => val(2) }) } of Myst::Value => Myst::Value)]
it_interprets %q({<{a: 1}>: {b: 2}}), [TMap.new({ TMap.new({ val(:a) => val(1) }) => TMap.new({ val(:b) => val(2) }) } of MTValue => MTValue)]
end
32 changes: 16 additions & 16 deletions spec/interpreter/value_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@ require "../spec_helper.cr"
describe "Values" do
describe "::from_literal" do
it "maps NilLiteral to TNil" do
Myst::Value.from_literal(NilLiteral.new).should be_a(TNil)
MTValue.from_literal(NilLiteral.new).should be_a(TNil)
end

it "maps BooleanLiteral to TBoolean" do
Myst::Value.from_literal(BooleanLiteral.new(false)).should be_a(TBoolean)
MTValue.from_literal(BooleanLiteral.new(false)).should be_a(TBoolean)
end

it "maps IntegerLiteral to TInteger" do
Myst::Value.from_literal(IntegerLiteral.new("0")).should be_a(TInteger)
MTValue.from_literal(IntegerLiteral.new("0")).should be_a(TInteger)
end

it "maps FloatLiteral to TFloat" do
Myst::Value.from_literal(FloatLiteral.new("0.0")).should be_a(TFloat)
MTValue.from_literal(FloatLiteral.new("0.0")).should be_a(TFloat)
end

it "maps StringLiteral to TString" do
Myst::Value.from_literal(StringLiteral.new("hello")).should be_a(TString)
MTValue.from_literal(StringLiteral.new("hello")).should be_a(TString)
end

it "maps SymbolLiteral to TSymbol" do
Myst::Value.from_literal(SymbolLiteral.new("hi")).should be_a(TSymbol)
MTValue.from_literal(SymbolLiteral.new("hi")).should be_a(TSymbol)
end

# Container values like List and Map require some effort from the
# interpreter to be generated. As such, Value::from_literal cannot generate
# them automatically from a node.
it "does not map ListLiterals" do
expect_raises(Exception) { Myst::Value.from_literal(ListLiteral.new).should be_a(TList) }
expect_raises(Exception) { MTValue.from_literal(ListLiteral.new).should be_a(TList) }
end

it "does not map MapLiterals" do
expect_raises(Exception) { Myst::Value.from_literal(MapLiteral.new).should be_a(TMap) }
expect_raises(Exception) { MTValue.from_literal(MapLiteral.new).should be_a(TMap) }
end
end

Expand Down Expand Up @@ -239,15 +239,15 @@ hi
end

it "can be created with initial elements" do
TList.new([TNil.new, TNil.new] of Myst::Value)
TList.new([TNil.new, TNil.new] of MTValue)
end

it "can contain any mixture of Values" do
TList.new([TInteger.new(1_i64), TBoolean.new(false), TString.new("hello")])
end

it "can contain other lists within itself" do
TList.new([TList.new, TList.new] of Myst::Value)
TList.new([TList.new, TList.new] of MTValue)
end

it "can dynamically adjust its size" do
Expand All @@ -260,8 +260,8 @@ hi

it "is always truthy" do
TList.new.truthy?.should eq(true)
TList.new([TNil.new] of Myst::Value).truthy?.should eq(true)
TList.new([TBoolean.new(false)] of Myst::Value).truthy?.should eq(true)
TList.new([TNil.new] of MTValue).truthy?.should eq(true)
TList.new([TBoolean.new(false)] of MTValue).truthy?.should eq(true)
TList.new([TInteger.new(1_i64), TNil.new]).truthy?.should eq(true)
end
end
Expand All @@ -273,15 +273,15 @@ hi
end

it "can be created with initial elements" do
TMap.new({ TNil.new => TNil.new } of Myst::Value => Myst::Value)
TMap.new({ TNil.new => TNil.new } of MTValue => MTValue)
end

it "can contain any mixture of Values" do
TMap.new({ TInteger.new(1_i64) => TBoolean.new(false), TString.new("hello") => TSymbol.new("hi")})
end

it "can contain other maps within itself" do
TMap.new({ TMap.new => TMap.new } of Myst::Value => Myst::Value)
TMap.new({ TMap.new => TMap.new } of MTValue => MTValue)
end

it "can dynamically adjust its size" do
Expand All @@ -294,8 +294,8 @@ hi

it "is always truthy" do
TMap.new.truthy?.should eq(true)
TMap.new({ TNil.new => TNil.new } of Myst::Value => Myst::Value).truthy?.should eq(true)
TMap.new({ TSymbol.new("") => TInteger.new(1_i64) } of Myst::Value => Myst::Value).truthy?.should eq(true)
TMap.new({ TNil.new => TNil.new } of MTValue => MTValue).truthy?.should eq(true)
TMap.new({ TSymbol.new("") => TInteger.new(1_i64) } of MTValue => MTValue).truthy?.should eq(true)
end
end
end
4 changes: 2 additions & 2 deletions spec/support/breakpoints.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ require "../spec_helper.cr"
# end
# ), interpreter: itr
macro add_breakpoint(itr, name)
%handler = ->(this : Myst::Value, __args : Array(Myst::Value), block : TFunctor?) do
%handler = ->(this : MTValue, __args : Array(MTValue), block : TFunctor?) do
%result = begin
{{ yield }}
end

%result.is_a?(Myst::Value) ? %result : TNil.new.as(Myst::Value)
%result.is_a?(MTValue) ? %result : TNil.new.as(MTValue)
end

{{itr}}.kernel.scope[{{name}}] = TFunctor.new({{name}}, [%handler] of Callable)
Expand Down
14 changes: 7 additions & 7 deletions spec/support/interpret.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../spec_helper.cr"
require "./nodes.cr"

def it_interprets(node : String, expected_stack : Array(Myst::Value), itr=Interpreter.new, file=__FILE__, line=__LINE__, end_line=__END_LINE__)
def it_interprets(node : String, expected_stack : Array(MTValue), itr=Interpreter.new, file=__FILE__, line=__LINE__, end_line=__END_LINE__)
it %Q(interprets #{node}), file, line, end_line do
program = parse_program(node)
itr.run(program)
Expand Down Expand Up @@ -31,11 +31,11 @@ def it_interprets(node : String, file=__FILE__, line=__LINE__, end_line=__END_LI
end

def it_interprets(node : String, file=__FILE__, line=__LINE__, end_line=__END_LINE__)
it_interprets(node, [] of Myst::Value, Interpreter.new, file, line, end_line)
it_interprets(node, [] of MTValue, Interpreter.new, file, line, end_line)
end


def it_interprets_with_assignments(node : String, assignments : Hash(String, Myst::Value), itr=Interpreter.new, file=__FILE__, line=__LINE__, end_line=__END_LINE__)
def it_interprets_with_assignments(node : String, assignments : Hash(String, MTValue), itr=Interpreter.new, file=__FILE__, line=__LINE__, end_line=__END_LINE__)
it %Q(interprets #{node}), file, line, end_line do
program = parse_program(node)
itr.run(program)
Expand Down Expand Up @@ -99,21 +99,21 @@ end

# val(node)
#
# Run `Value.from_literal` on the given node and return the result. If `node`
# Run `MTValue.from_literal` on the given node and return the result. If `node`
# is not already a Node, it will be run through `l` first.
def val(node : Node)
Myst::Value.from_literal(node).as(Myst::Value)
MTValue.from_literal(node).as(MTValue)
end

def val(node : Array(T)) forall T
TList.new(node.map{ |n| val(n) }).as(Myst::Value)
TList.new(node.map{ |n| val(n) }).as(MTValue)
end

def val(node : Hash(K, V)) forall K, V
node.reduce(TMap.new) do |map, (k, v)|
map.entries[val(k)] = val(v)
map
end.as(Myst::Value)
end.as(MTValue)
end

def val(node); val(l(node)); end
12 changes: 6 additions & 6 deletions src/myst/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ require "./interpreter/native_lib"

module Myst
class Interpreter
property stack : Array(Value)
property self_stack : Array(Value)
property stack : Array(MTValue)
property self_stack : Array(MTValue)
property scope_stack : Array(Scope)
property callstack : Callstack
property kernel : TModule
Expand All @@ -22,11 +22,11 @@ module Myst
2 => errput
})

@stack = [] of Value
@stack = [] of MTValue
@scope_stack = [] of Scope
@callstack = Callstack.new
@kernel = create_kernel
@self_stack = [@kernel] of Value
@self_stack = [@kernel] of MTValue
@warnings = 0
end

Expand Down Expand Up @@ -85,7 +85,7 @@ module Myst
self_stack.last
end

def push_self(new_self : Value)
def push_self(new_self : MTValue)
self_stack.push(new_self)
end

Expand Down Expand Up @@ -119,7 +119,7 @@ module Myst

def put_error(error : RuntimeError)
value_to_s = __scopeof(error.value)["to_s"].as(TFunctor)
result = Invocation.new(self, value_to_s, error.value, [] of Value, nil).invoke
result = Invocation.new(self, value_to_s, error.value, [] of MTValue, nil).invoke
errput.puts("Uncaught Exception: " + result.as(TString).value)
errput.puts(error.trace)
end
Expand Down
6 changes: 3 additions & 3 deletions src/myst/interpreter/closure_scope.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Myst
property closed_scope : Scope

def initialize(@closed_scope : Scope, @parent : Scope? = nil)
@values = {} of String => Value
@values = {} of String => MTValue
end

def []?(key : String)
Expand All @@ -20,7 +20,7 @@ module Myst
end
end

def []=(key : String, value : Value)
def []=(key : String, value : MTValue)
if closed_scope.has_key?(key)
closed_scope.assign(key, value)
else
Expand All @@ -32,7 +32,7 @@ module Myst
!!@values[key]? || closed_scope.has_key?(key)
end

def assign(key : String, value : Value)
def assign(key : String, value : MTValue)
if closed_scope.has_key?(key)
closed_scope.assign(key, value)
else
Expand Down
4 changes: 2 additions & 2 deletions src/myst/interpreter/exceptions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module Myst

# The containing error type for any error raised within the language.
class RuntimeError < Exception
property value : Value
property value : MTValue
property trace : Callstack

def initialize(@value : Value, @trace : Callstack)
def initialize(@value : MTValue, @trace : Callstack)
end
end

Expand Down
8 changes: 4 additions & 4 deletions src/myst/interpreter/invocation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ module Myst
struct Invocation
property itr : Interpreter
property func : TFunctor
property! receiver : Value?
property args : Array(Value)
property! receiver : MTValue?
property args : Array(MTValue)
property! block : TFunctor?
@selfstack_size_at_entry : Int32 = -1
@scopestack_size_at_entry : Int32 = -1
@callstack_size_at_entry : Int32 = -1

def initialize(@itr : Interpreter, @func : TFunctor, @receiver : Value?, @args : Array(Value), @block : TFunctor?)
def initialize(@itr : Interpreter, @func : TFunctor, @receiver : MTValue?, @args : Array(MTValue), @block : TFunctor?)
end

def invoke
Expand Down Expand Up @@ -113,7 +113,7 @@ module Myst
return @itr.stack.pop
end

private def do_call(func : TNativeDef, receiver : Value, args : Array(Value), block : TFunctor?)
private def do_call(func : TNativeDef, receiver : MTValue, args : Array(MTValue), block : TFunctor?)
func.call(receiver, args, block)
end

Expand Down
8 changes: 4 additions & 4 deletions src/myst/interpreter/matcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "./exceptions.cr"

module Myst
class Interpreter
def match(pattern : Node, value : Value)
def match(pattern : Node, value : MTValue)
case pattern
when ListLiteral
match_list(pattern, value)
Expand Down Expand Up @@ -31,7 +31,7 @@ module Myst
# For simplicity and efficiency, the equality of values according to a
# match operation is determined by the native equality of the values, not
# by any override of `==`.
private def match_value(pattern : Node, right : Value)
private def match_value(pattern : Node, right : MTValue)
visit(pattern)
left = stack.pop
success =
Expand All @@ -51,7 +51,7 @@ module Myst
success || __raise_runtime_error(MatchError.new(callstack))
end

private def match_list(pattern : ListLiteral, value : Value)
private def match_list(pattern : ListLiteral, value : MTValue)
__raise_runtime_error(MatchError.new(callstack)) unless value.is_a?(TList)

left, splat, right = chunk_list_pattern(pattern)
Expand All @@ -68,7 +68,7 @@ module Myst
end
end

private def match_map(pattern : MapLiteral, value : Value)
private def match_map(pattern : MapLiteral, value : MTValue)
__raise_runtime_error(MatchError.new(callstack)) unless value.is_a?(TMap)

pattern.entries.each do |entry|
Expand Down
Loading

0 comments on commit 2efe944

Please sign in to comment.