Skip to content

Commit

Permalink
support message variants
Browse files Browse the repository at this point in the history
This commit adds basic support for message variants.  Variants
are placed in `variant/*.cmake` files.  Variants can include
different sections of the message.  There is rudimentary support
for encoding differences.  Currently the only option supported
is whether the `|` and `$` syntax is considered to be syntactic
sugar that needs expanding into parentheses, or genuine symbols
to be communicated.
  • Loading branch information
paulfitz committed Aug 16, 2016
1 parent 1e50a96 commit 5a82298
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 34 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -110,6 +110,18 @@ To build the message, type:
You should find the message saved in your build directory
as `index.json` and `index.txt`.

Compilation options
-------------------

There are options in how the message is built. Run the cmake gui (`cmake-gui` or `ccmake` in Linux)
and look for options starting with `COSMIC_`.

* `COSMIC_VARIANT` set to `default` - this currently assumes that symbols like `$` and `|` can be
somehow represented in the encoded message.
* `COSMIC_VARIANT` set to `nested` - this converts the `$` and `|` symbols to parentheses.
* `COSMIC_LINES` controls how many lines of the message are processed. The default is 0, meaning
unlimited.

Troubleshooting
---------------
If you have compilation issues with `nodejs` similar to `node returned No such file or directory`, this can be resolved by symlinking it to `node`. Another workaround is to install `nodejs-legacy`.
Expand Down
31 changes: 22 additions & 9 deletions transform/CMakeLists.txt
Expand Up @@ -35,13 +35,14 @@ add_custom_command(OUTPUT ${WORK}/CosmicEval.js
COMMAND ${HAXE} -js CosmicEval.js -main cosmicos.Evaluate -cp ${SRC}
WORKING_DIRECTORY ${WORK}
DEPENDS
${SRC}/cosmicos/BitString.hx
${SRC}/cosmicos/Config.hx
${SRC}/cosmicos/CosFunction.hx
${SRC}/cosmicos/Evaluate.hx
${SRC}/cosmicos/ManuscriptStyle.hx
${SRC}/cosmicos/Memory.hx
${SRC}/cosmicos/Parse.hx
${SRC}/cosmicos/Vocab.hx
${SRC}/cosmicos/Memory.hx
${SRC}/cosmicos/ManuscriptStyle.hx
${SRC}/cosmicos/CosFunction.hx
${SRC}/cosmicos/BitString.hx
)

add_custom_command(OUTPUT ${WORK}/SpiderScrawl.js
Expand All @@ -68,8 +69,18 @@ add_custom_target(jshelpers ALL DEPENDS
## Add targets for message parts
##

# we expect to get a variable COSMIC_DEPENDS that lists all parts
include(${CMAKE_SOURCE_DIR}/src/README.cmake)
set(COSMIC_VARIANT "default" CACHE STRING "Version of message to build")
set(COSMIC_LINES "0" CACHE INT "Number of lines to process (0 for unlimited)")
set_property(
CACHE COSMIC_VARIANT
PROPERTY STRINGS
"default" "nested" # should read this from variant directory once there
# are more variants to support
)
# We expect to get these variables:
# COSMIC_DEPENDS that lists all parts
# COSMIC_USE_FLATTENER that controls whether '|' can be encoded
include(${CMAKE_SOURCE_DIR}/variant/${COSMIC_VARIANT}.cmake)

get_target_property(UnlessDriverLoc UnlessDriver JAR_FILE)
get_target_property(FritzifierLoc Fritzifier JAR_FILE)
Expand All @@ -88,8 +99,8 @@ set(ACTIVE_DEPENDS)
set(ACTIVE_DEPENDS_SHORT "")
set(EXTRA_DEPEND_pl ${MSG}/cosmic.pm)
set(EXTRA_DEPEND_js ${MSG}/cosmic.js)
set(EXTRA_DEPEND_gate ${CMAKE_SOURCE_DIR}/bin/drawgate-ppm.pl ${CMAKE_SOURCE_DIR}/bin/drawgate-txt.pl ${UnlessDriverLoc})
set(EXTRA_DEPEND_java ${FritzifierLoc})
set(EXTRA_DEPEND_gate ${CMAKE_SOURCE_DIR}/bin/drawgate-ppm.pl ${CMAKE_SOURCE_DIR}/bin/drawgate-txt.pl UnlessDriver)
set(EXTRA_DEPEND_java Fritzifier)
foreach(depend ${COSMIC_DEPENDS})
foreach(ext pl scm gate java js)
if(EXISTS ${MSG}/${depend}.${ext})
Expand All @@ -108,6 +119,9 @@ foreach(depend ${COSMIC_DEPENDS})
endforeach()
endforeach()

set(COSMIC_OPTION_FILE ${WORK}/config.json)
configure_file(config.json ${COSMIC_OPTION_FILE} @ONLY)

# assem.txt contains a concatenation of all message parts, in
# original textual form
add_custom_command(OUTPUT ${WORK}/assem.txt
Expand Down Expand Up @@ -223,4 +237,3 @@ add_custom_target(cli ALL DEPENDS ${CMAKE_BINARY_DIR}/bin/cosh.js)
##

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/assemble/cosmicos ${CMAKE_BINARY_DIR}/bin/cosmsg.js @ONLY)

30 changes: 7 additions & 23 deletions transform/assemble/evaluate.js
Expand Up @@ -3,10 +3,9 @@ var assert = require('assert');
var cos = require("CosmicEval").cosmicos;

var all = JSON.parse(fs.readFileSync("assem.json", 'utf8'));
// var all = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
var config = new cos.Config(fs.readFileSync("config.json", 'utf8'));


var ev = new cos.Evaluate();
var ev = new cos.Evaluate(config);
ev.applyOldOrder();

try {
Expand All @@ -33,17 +32,15 @@ function run(op,part,skippy) {
txt += "\n";
if (skippy) return 1;
var v = ev.evaluateLine(op);
//console.log(JSON.stringify(cos.Parse.deconsify(v),ev.vocab));
//console.log(v);
return v;
}

var err_part = null;
var err_i = -1;
var line_limit = config.lines();
try {
//throw "skip it all";
var cline = 0;
for (var i=0; i<all.length && i<5000; i++) {
for (var i=0; i<all.length && (i<line_limit || line_limit==0); i++) {
var part = all[i];
err_part = part;
err_i = i;
Expand Down Expand Up @@ -71,24 +68,14 @@ try {
process.stderr.write("At " + i + "\n");
}

/*
if (op[0] == '(') {
op = op.replace(/^\(/,"");
op = op.replace(/\);/,"");
if (part.lines.length>0) {
part.lines[0] = part.lines[0].replace(/^\(/,"");
part.lines[part.lines.length-1] = part.lines[part.lines.length-1].replace(/\);/,";");
}
}
*/

op = ev.preprocessLine(op);
part['preprocessed'] = op;
var v = run(op,part,skippy);

if (op.indexOf("demo ")==0) {
var r = cos.Parse.recover(cos.Parse.deconsify(v));
console.log("Evaluated to: " + r);
op = "equal " + r + " " + op.substr(5,op.length);
// v = run(op); // will need a separate pass for this
part["lines_original"] = part["lines"];
part["lines"] = [ "(" + op + ");" ];
part["code"] = ev.codifyLine(op);
Expand All @@ -104,8 +91,7 @@ try {
}
} catch (e) {
process.stderr.write("* evaluate.js failed on " + err_i + ": " + JSON.stringify(err_part) + "\n");
// continue for now, to compare with old version
//throw(e);
throw(e);
}


Expand Down Expand Up @@ -140,8 +126,6 @@ for (var i=0; i<all.length; i++) {
ct++;
}

//console.log(txt);
//txt = txt.match(/.{1,80}/g).join("\n");
fs.writeFileSync('q.txt',txt);
fs.writeFileSync('assem2.json',JSON.stringify(all, null, 2));

Expand Down
6 changes: 6 additions & 0 deletions transform/config.json
@@ -0,0 +1,6 @@
{
"variant": "@COSMIC_VARIANT@",
"use_flattener": @COSMIC_USE_FLATTENER@,
"lines": @COSMIC_LINES@
}

38 changes: 38 additions & 0 deletions transform/cosmicos/Config.hx
@@ -0,0 +1,38 @@
// -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-

package cosmicos;

@:expose
class Config {
private var config : Dynamic;

public function new(txt: String = null) {
config = null;
if (txt != null) {
config = haxe.Json.parse(txt);
}
}

/**
*
* Option: is flattener syntax "|" and "$" supported in the message or should
* these be mapped to parentheses.
*
*/
public function useFlattener() : Bool {
if (config == null) return true;
return Reflect.field(config, 'use_flattener');
}

/**
*
*
* Number of lines of message to work on - 0 means unlimited. Can be convenient
* to set to a small number during development.
*
*/
public function lines() : Int {
if (config == null) return 0;
return Reflect.field(config, 'lines');
}
}
14 changes: 13 additions & 1 deletion transform/cosmicos/Evaluate.hx
Expand Up @@ -4,6 +4,7 @@ package cosmicos;

@:expose
class Evaluate {
private var config : Config;
private var vocab : Vocab;
private var mem : Memory;
private var id_lambda : Dynamic;
Expand Down Expand Up @@ -181,6 +182,13 @@ class Evaluate {
return lst;
}

public function preprocessLine(str: String) : String {
if (!config.useFlattener()) {
str = Parse.removeFlatteningSyntax(str);
}
return str;
}

public function codifyLine(str: String) : String {
var lst = Parse.stringToList(str,vocab);
Parse.encodeSymbols(lst,vocab);
Expand All @@ -194,7 +202,7 @@ class Evaluate {
return lst;
}

public function new() {
public function new(config: Config = null) {
mem = new Memory(null);
vocab = new Vocab();
id_lambda = vocab.get("?");
Expand All @@ -203,6 +211,10 @@ class Evaluate {
id_if = vocab.get("if");
id_assign = vocab.get("assign");
id_translate = -1;
this.config = config;
if (config == null) {
this.config = new Config();
}
}

static private function isBi(x:Dynamic) : Bool {
Expand Down
2 changes: 1 addition & 1 deletion transform/cosmicos/Parse.hx
Expand Up @@ -132,7 +132,7 @@ class Parse {
}


public static function preprocessString(x: String) : String {
public static function removeFlatteningSyntax(x: String) : String {
var res = removePipes(x, 0); // start at level zero and remove | elements
res = removeDollars(res); // transform $x into (x)
return res;
Expand Down
6 changes: 6 additions & 0 deletions variant/default.cmake
@@ -0,0 +1,6 @@
# we expect to get a variable COSMIC_DEPENDS that lists all parts
include(${CMAKE_SOURCE_DIR}/src/README.cmake)

# Flag controlling whether the "|" symbol for flattening messages can be
# encoded (if false), or (if false) should be expanded to nested parens.
set(COSMIC_USE_FLATTENER true)
6 changes: 6 additions & 0 deletions variant/nested.cmake
@@ -0,0 +1,6 @@
# we expect to get a variable COSMIC_DEPENDS that lists all parts
include(${CMAKE_SOURCE_DIR}/src/README.cmake)

# Flag controlling whether the "|" symbol for flattening messages can be
# encoded (if true), or (if false) should be expanded to nested parens.
set(COSMIC_USE_FLATTENER false)

0 comments on commit 5a82298

Please sign in to comment.