From a7c9f2b8c344ed825f08dcda0215e1a0718dcfc6 Mon Sep 17 00:00:00 2001 From: Danny McClanahan <1305167+cosmicexplorer@users.noreply.github.com> Date: Sun, 10 Nov 2024 06:12:01 -0500 Subject: [PATCH] add output testing script for debugging compiles --- semantics.coffee | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 semantics.coffee diff --git a/semantics.coffee b/semantics.coffee new file mode 100644 index 0000000000..619b5104a7 --- /dev/null +++ b/semantics.coffee @@ -0,0 +1,55 @@ +assert = require 'assert' +process = require 'process' +coffee = require './lib/coffeescript/index.js' + +[input, ...] = process.argv[2..] + + +rawTokens = (input) -> {type, value} for [type, value] in coffee.tokens input + +encodeTokens = (input) -> for {type, value} in rawTokens input + value = value.toString() + if type.match(/^[A-Z_]+$/) or type is 'BIN?' + if value.match /^\s+$/ + {whitespace: type, value: encodeURIComponent value} + else + {type, value} + else + assert (type is value), JSON.stringify {type, value} + {punct: value} + +tokenPrint = (input) -> for {punct, whitespace, type, value} in encodeTokens input + if punct? + ": #{punct}" + else if whitespace? + "#{whitespace}('#{value}')" + else + "#{type}(#{value})" + +nodes = (input) -> coffee.nodes input +# .body.expressions[0].params[0].name.properties[0] + +compiled = (input) -> coffee.compile input, bare: yes + +evaled = (input) -> coffee.eval input + + +{TOK, AST, AST_PATH, COMP, EV} = process.env +output = if TOK? + switch TOK + when 'raw' then rawTokens input + when 'enc' then encodeTokens input + else (tokenPrint input).join '\n' +else if AST? + ret = nodes input + if AST_PATH? + eval "ret#{AST_PATH}" + else + ret +else if COMP? + compiled input +else if EV? + evaled input +else throw new Error('wtf') + +console.log output