Skip to content

Commit

Permalink
add output testing script for debugging compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Nov 15, 2024
1 parent 817c39a commit a7c9f2b
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions semantics.coffee
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a7c9f2b

Please sign in to comment.