Skip to content

Commit

Permalink
More namespace stuff... also scripting now.
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesphere committed Feb 20, 2012
1 parent d0a0f19 commit c354af6
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 24 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -66,13 +66,12 @@ There are a few things that differentiate silica from Clay. Here's an extremely
### To do:
Here's a short list of things to do:

* Rewrite MetaCommand to take a single parameter
* Change NamespaceTable to a tree (allow foo::bar and foo::baz::bar, for example)
* Transforms
* Scale switching
* Meta commands
* Interpreter
* Persistence
* Scripting
* Standard Modes/Scales
* Error handling
* Concurrent melodic lines
Expand Down
4 changes: 4 additions & 0 deletions examples/test.silica
@@ -0,0 +1,4 @@
hat >> play rp play lp play
hat3 = hat rp hat lp hat
fn_do3(x) := 3x
fn_do3(hat3)
5 changes: 5 additions & 0 deletions lang/MetaCommand.io
Expand Up @@ -5,6 +5,11 @@
if(?REPL_DEBUG, writeln(" + Loading MetaCommand.io"))

silica MetaCommand := silica Primitive clone do(
execute := method(params,
out := self behavior call(params)
out
)

asString := method(
"< METACOMMAND " .. self name .. " >"
)
Expand Down
7 changes: 7 additions & 0 deletions lang/Namespace.io
Expand Up @@ -7,10 +7,17 @@ if(?REPL_DEBUG, writeln(" + Loading Namespace.io"))
silica Namespace := Object clone do(
name ::= nil
parent ::= nil
children ::= nil

init := method(
self name = nil
self parent = nil
self children = list
)

addChild := method(child,
self children append(child)
self
)

with := method(name, parent,
Expand Down
39 changes: 35 additions & 4 deletions lang/TokenTable.io
Expand Up @@ -47,7 +47,24 @@ silica TokenTable := Object clone do(
self add(home, "-state", silica MetaCommand with("-STATE", block("-STATE\n" .. silica Note asString)))
self add(home, "-reset", silica MetaCommand with("-RESET", block("-RESET\n" .. silica Note reset asString)))
self add(home, "-@?" , silica MetaCommand with("-@?",
block("-@?\nCurrently in namespace \"" .. silica REPL REPL currentNamespace name .. "\"")
block(
ns := silica REPL REPL currentNamespace
out := "-@?\nCurrently in namespace \"" .. ns name .. "\"\n"
if(ns parent == nil,
out = out .. "This namespace is the root, and has no parent.\n"
,
out = out .. "This namespace's parent is \"" .. ns parent name .. "\"\n"
)
if(ns children size == 0,
out = out .. "This namespace contains no children."
,
out = out .. "This namespace's children are:"
ns children foreach(child,
out = out .. "\n" .. " " .. child name
)
)
out
)
))
self add(home, "-debug", silica MetaCommand with("-DEBUG",
block(
Expand All @@ -71,9 +88,23 @@ silica TokenTable := Object clone do(
out
)
))



self add(home, "-enter", silica MetaCommand with("-ENTER",
block(ns,
out := "-ENTER\n"
namespace := silica namespace(ns)
if(namespace == nil,
silica NamespaceTable new(ns, silica REPL REPL currentNamespace)
writeln(ns)
namespace := silica namespace(ns)
writeln(namespace)
silica REPL REPL currentNamespace addChild(namespace)
writeln(silica REPL REPL currentNamespace children)
)
silica REPL REPL currentNamespace = namespace
out = out .. "Entering namespace \"" .. namespace name .. "\""
out
)
))
)

asString := method(
Expand Down
59 changes: 42 additions & 17 deletions repl/REPL.io
Expand Up @@ -31,14 +31,33 @@ silica REPL REPL := Object clone do(
)

// runs the whole thing
run := method(
loop(
in := self rl readLine(self rl prompt);
self rl addHistory(in);
parse(in);
if(silica exit,
self saveHistory;
break
run := method(script,
if(script != nil,
// scripting mode
file := File with(script) openForReading
writeln("Running script \"" .. file path .. "\".\n\n")
loop(
in := file readLine
if(in == nil,
silica exit = true;
break
)
if(in strip == "",
continue
)
parse(in)
)
file close
,
// interactive mode
loop(
in := self rl readLine(self rl prompt);
self rl addHistory(in);
parse(in);
if(silica exit,
self saveHistory;
break
)
)
)
)
Expand All @@ -53,7 +72,7 @@ silica REPL REPL := Object clone do(
)
if(?REPL_DEBUG, writeln("TRACE (parse) received: " .. processed))
processed splitNoEmpties foreach(tok,
ret := self interpretToken(tok asMutable lowercase, true)
ret := self interpretToken(tok asMutable lowercase, true, self currentNamespace)
if(ret != nil, out append(ret))
)
if(out size == 1, out append("okay"))
Expand Down Expand Up @@ -122,7 +141,7 @@ silica REPL REPL := Object clone do(
out append(tok afterSeq("+"))
,
// not a grouping factor or repetition factor
ret := self interpretToken(tok asMutable lowercase, false)
ret := self interpretToken(tok asMutable lowercase, false, self currentNamespace)
if(ret == nil,
writeln("--> ERROR: cannot recognize token \"" .. tok asMutable uppercase .. "\" within namespace \"" .. self currentNamespace name .. "\".")
break
Expand Down Expand Up @@ -152,12 +171,12 @@ silica REPL REPL := Object clone do(
out join(" ")
)

interpretToken := method(token, final,
interpretToken := method(token, final, namespace,
// find function, if it is one
tokenName := token beforeSeq("(")

// get token
itok := silica token(self currentNamespace, tokenName)
itok := silica token(namespace, tokenName)

// function?
if(itok isKindOf(silica Function),
Expand All @@ -175,7 +194,9 @@ silica REPL REPL := Object clone do(
// meta?
if(itok isKindOf(silica MetaCommand),
if(final,
return self interpretMetaCommand(itok)
param := token afterSeq("(")
if(param != nil, param = param beforeSeq(")"))
return self interpretMetaCommand(itok, param)
,
return token
)
Expand All @@ -195,8 +216,12 @@ silica REPL REPL := Object clone do(
return token
)

// uninterpretable
return nil
// uninterpretable.. jump up a namespace
if(namespace parent != nil,
self interpretToken(token, final, namespace parent)
,
return nil
)
)

interpretFunction := method(fn, params,
Expand All @@ -215,9 +240,9 @@ silica REPL REPL := Object clone do(
out
)

interpretMetaCommand := method(meta,
interpretMetaCommand := method(meta, param,
if(?REPL_DEBUG, writeln("TRACE (interpretToken): MetaCommand found: " .. meta))
out := meta execute
out := meta execute(param)
"META> " .. out
)
)
2 changes: 1 addition & 1 deletion repl/common.io
Expand Up @@ -27,4 +27,4 @@ if(?REPL_DEBUG, writeln("Initializing REPL..."))

if(?REPL_DEBUG, writeln(" + Starting REPL..."))
silica exit := false
silica REPL REPL run
silica REPL REPL run(SCRIPT_FILE)
3 changes: 3 additions & 0 deletions silica.io
Expand Up @@ -4,6 +4,9 @@
// Jacob M. Peck
// silica runner

// if run as a script, this will not be nil, but a file
SCRIPT_FILE := System args at(1)

// load libraries
doFile("lib/io-symbols.io")

Expand Down

0 comments on commit c354af6

Please sign in to comment.