-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0a399ba
Showing
16 changed files
with
2,015 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Copyright (c) 2014 Branden J Brown | ||
|
||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. | ||
|
||
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: | ||
|
||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. | ||
|
||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | ||
|
||
3. This notice may not be removed or altered from any source distribution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# iolang | ||
|
||
This is an attempt at a pure Go implementation of [Io](http://iolanguage.org/) with a focus on embedding into Go programs. Much of the hard work has been done, but hard work still remains. | ||
|
||
The current way to embed an iolang interpreter in a Go program involves using the `NewVM()` function to get a `*VM` and using `SetSlot()` to make available any extras, then feeding the VM things to evaluate with its `DoString()` and `DoReader()` methods. The VM also has methods like `NewNumber()`, `NewString()`, `ObjectWith()`, &c. to create primitives. The current API is actually pretty awful and will change, however. Most likely everything but `NewVM()` will be a method of the VM. | ||
|
||
The `io` directory contains an interactive interpreter as an example of embedding iolang. It also proves to me that something is currently wrong either with parsing or evaluating, because only numbers and strings work correctly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package iolang | ||
|
||
type Block struct { | ||
Object | ||
Message *Message | ||
Self Interface | ||
ArgNames []string | ||
Activatable bool | ||
} | ||
|
||
func (b *Block) Activate(vm *VM, target, locals Interface, msg *Message) Interface { | ||
// If this block isn't actually activatable, then it should be the result | ||
// of evaluation. | ||
if !b.Activatable { | ||
return b | ||
} | ||
return b.reallyActivate(vm, target, locals, msg) | ||
} | ||
|
||
func (b *Block) reallyActivate(vm *VM, target, locals Interface, msg *Message) Interface { | ||
blkLocals := &Object{Slots: Slots{}, Protos: []Interface{vm.BaseObject}} | ||
for i, arg := range b.ArgNames { | ||
if x := msg.ArgAt(i); x != nil { | ||
SetSlot(blkLocals, arg, x) | ||
} else { | ||
SetSlot(blkLocals, arg, vm.Nil) | ||
} | ||
} | ||
scope := b.Self | ||
if scope == nil { | ||
scope = target | ||
} | ||
call := vm.NewCall(locals, b, msg, target) | ||
SetSlot(blkLocals, "self", scope) | ||
SetSlot(blkLocals, "call", call) | ||
result := b.Message.Eval(vm, blkLocals) | ||
if stop, ok := result.(Stop); ok { | ||
return stop.Result | ||
} | ||
return result | ||
} | ||
|
||
func ObjectBlock(vm *VM, target, locals Interface, msg *Message) Interface { | ||
blk := Block{ | ||
Object: Object{Slots: vm.DefaultSlots["block"], Protos: []Interface{vm.BaseObject}}, | ||
Message: msg.ArgAt(len(msg.Args) - 1), | ||
ArgNames: make([]string, len(msg.Args)-1), | ||
} | ||
for i, arg := range msg.Args[:len(msg.Args)-1] { | ||
blk.ArgNames[i] = arg.Name() | ||
} | ||
return &blk | ||
} | ||
|
||
func ObjectMethod(vm *VM, target, locals Interface, msg *Message) Interface { | ||
blk := ObjectBlock(vm, target, locals, msg) | ||
blk.(*Block).Activatable = true | ||
return blk | ||
} | ||
|
||
func BlockCall(vm *VM, target, locals Interface, msg *Message) Interface { | ||
return target.(*Block).reallyActivate(vm, target, locals, msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package iolang | ||
|
||
func (vm *VM) initTrue() { | ||
vm.True.Protos = []Interface{vm.BaseObject} | ||
s := vm.NewString("true") | ||
vm.True.Slots = Slots{ | ||
"asSimpleString": s, | ||
"asString": s, | ||
"else": vm.True, | ||
"elseif": vm.True, | ||
"ifFalse": vm.True, | ||
"ifTrue": vm.NewCFunction(ObjectEvalArgAndReturnSelf, "ObjectEvalArgAndReturnSelf(msg)"), | ||
"not": vm.False, | ||
"or": vm.True, | ||
"type": s, | ||
} | ||
} | ||
|
||
func (vm *VM) initFalse() { | ||
vm.False.Protos = []Interface{vm.BaseObject} | ||
s := vm.NewString("false") | ||
vm.False.Slots = Slots{ | ||
"and": vm.False, | ||
"asSimpleString": s, | ||
"asString": s, | ||
"else": vm.NewCFunction(ObjectEvalArgAndReturnNil, "ObjectEvalArgAndReturnNil(msg)"), | ||
"elseif": vm.NewCFunction(ObjectIf, "ObjectIf(cond, onTrue, onFalse)"), | ||
"ifFalse": vm.NewCFunction(ObjectEvalArgAndReturnSelf, "ObjectEvalArgAndReturnSelf(msg)"), | ||
"ifTrue": vm.False, | ||
"isTrue": vm.False, | ||
"not": vm.True, | ||
// TODO: or | ||
"then": vm.False, | ||
"type": s, | ||
} | ||
} | ||
|
||
func (vm *VM) initNil() { | ||
vm.Nil.Protos = []Interface{vm.BaseObject} | ||
s := vm.NewString("nil") | ||
vm.Nil.Slots = Slots{ | ||
"and": vm.False, | ||
"asSimpleString": s, | ||
"asString": s, | ||
"else": vm.Nil, | ||
"elseif": vm.Nil, | ||
"ifNil": vm.NewCFunction(ObjectEvalArgAndReturnSelf, "ObjectEvalArgAndReturnSelf(msg)"), | ||
"ifNilEval": vm.NewCFunction(ObjectEvalArg, "ObjectEvalArg(msg)"), | ||
"ifNonNil": vm.Nil, // TODO: Io calls "Object_thisContext()" | ||
"ifNonNilEval": vm.Nil, // TODO: same | ||
"isNil": vm.True, | ||
"isTrue": vm.False, | ||
"not": vm.True, | ||
// TODO: or | ||
"then": vm.Nil, | ||
"type": s, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package iolang | ||
|
||
type Call struct { | ||
Object | ||
Sender Interface | ||
Activated Actor | ||
Msg *Message | ||
Target Interface | ||
} | ||
|
||
func (vm *VM) NewCall(sender Interface, actor Actor, msg *Message, target Interface) *Call { | ||
return &Call{ | ||
Object: Object{Slots: vm.DefaultSlots["Call"], Protos: []Interface{vm.BaseObject}}, | ||
Sender: sender, | ||
Activated: actor, | ||
Msg: msg, | ||
Target: target, | ||
} | ||
} | ||
|
||
func (vm *VM) initCall() { | ||
slots := Slots{ | ||
"activated": vm.NewCFunction(CallActivated, "CallActivated()"), | ||
"argAt": vm.NewCFunction(CallArgAt, "CallArgAt(n)"), | ||
"argCount": vm.NewCFunction(CallArgCount, "CallArgCount()"), | ||
"evalArgAt": vm.NewCFunction(CallEvalArgAt, "CallEvalArgAt(n)"), | ||
"message": vm.NewCFunction(CallMessage, "CallMessage()"), | ||
"sender": vm.NewCFunction(CallSender, "CallSender()"), | ||
"target": vm.NewCFunction(CallTarget, "CallTarget()"), | ||
} | ||
vm.DefaultSlots["Call"] = slots | ||
} | ||
|
||
func CallActivated(vm *VM, target, locals Interface, msg *Message) Interface { | ||
return target.(*Call).Activated | ||
} | ||
|
||
func CallArgAt(vm *VM, target, locals Interface, msg *Message) Interface { | ||
m := target.(*Call).Msg | ||
v, err := msg.NumberArgAt(vm, locals, 0) | ||
if err != nil { | ||
return vm.IoError(err) | ||
} | ||
return m.ArgAt(int(v.Value)) | ||
} | ||
|
||
func CallArgCount(vm *VM, target, locals Interface, msg *Message) Interface { | ||
return vm.NewNumber(float64(len(target.(*Call).Msg.Args))) | ||
} | ||
|
||
func CallEvalArgAt(vm *VM, target, locals Interface, msg *Message) Interface { | ||
m := target.(*Call).Msg | ||
v, err := msg.NumberArgAt(vm, locals, 0) | ||
if err != nil { | ||
return vm.IoError(err) | ||
} | ||
return m.EvalArgAt(vm, locals, int(v.Value)) | ||
} | ||
|
||
func CallMessage(vm *VM, target, locals Interface, msg *Message) Interface { | ||
return target.(*Call).Msg | ||
} | ||
|
||
func CallSender(vm *VM, target, locals Interface, msg *Message) Interface { | ||
return target.(*Call).Sender | ||
} | ||
|
||
func CallTarget(vm *VM, target, locals Interface, msg *Message) Interface { | ||
return target.(*Call).Target | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package iolang | ||
|
||
// A function which can be executed in the context of an Io VM. | ||
type Fn func(vm *VM, self, locals Interface, msg *Message) Interface | ||
|
||
// An object representing a statically compiled function, probably written | ||
// in Go for this implementation. | ||
type CFunction struct { | ||
Object | ||
Function Fn | ||
Name string | ||
} | ||
|
||
// Create a new CFunction wrapping f. | ||
func (vm *VM) NewCFunction(f Fn, name string) *CFunction { | ||
return &CFunction{ | ||
Object{Slots: vm.DefaultSlots["CFunction"], Protos: []Interface{vm.BaseObject}}, | ||
f, | ||
name, | ||
} | ||
} | ||
|
||
func (f *CFunction) Clone() Interface { | ||
return &CFunction{ | ||
Object{Slots: Slots{}, Protos: []Interface{f}}, | ||
f.Function, | ||
f.Name, | ||
} | ||
} | ||
|
||
// Call the wrapped function. | ||
func (f *CFunction) Activate(vm *VM, target, locals Interface, msg *Message) Interface { | ||
return f.Function(vm, target, locals, msg) | ||
} | ||
|
||
func (f *CFunction) String() string { | ||
return f.Name | ||
} |
Oops, something went wrong.