Some small fixes

This commit is contained in:
Mewrry the Kitty 2025-12-31 16:10:35 -06:00
parent 5767f131a1
commit c258f046b2

117
spinny.js
View file

@ -23,7 +23,7 @@ function onRun() {
} }
const fileSpincVersion = file.split("\n")[1].split("@"); const fileSpincVersion = file.split("\n")[1].split("@");
let isSpincFile = false; let isSpincFile = false;
if (fileSpincVersion.slice(0,2) === ["SPINC","spinny:itty" ]) { if (fileSpincVersion.slice(0,2).join("\n") === "SPINC\nspinny:itty") {
// This is a spinny source file // This is a spinny source file
if (fileSpincVersion[2] !== "noversion") { if (fileSpincVersion[2] !== "noversion") {
let niceversion = spileSpincVersion[2].replace( let niceversion = spileSpincVersion[2].replace(
@ -97,7 +97,7 @@ function showVersion(){
// . 1 . 2 . 3 . 4 . 5 . 6 // . 1 . 2 . 3 . 4 . 5 . 6
// ^ // ^
"Spinny for itty\n" + "Spinny for itty\n" +
"Version 0.0.1\n" + "Version 0.0.2\n" +
"Known spinc versions:\n" "Known spinc versions:\n"
" • `noversion'\n" " • `noversion'\n"
) )
@ -135,17 +135,17 @@ function parseCliArgs() {
} }
if (flag) { if (flag) {
if (arg === "compile" || arg === "c") { if (arg === "compile" || arg === "c") {
compileonly = true; compileOnly = true;
} else if (arg === "help" || arg === "h") { } else if (arg === "help" || arg === "h") {
showhelp(); showHelp();
quit(); quit();
} else if (arg === "version" || arg === "v") { } else if (arg === "version" || arg === "v") {
showversion(); showVersion();
quit(); quit();
} else if (arg === "output" || arg === "o") { } else if (arg === "output" || arg === "o") {
nextisoutput = true; nextIsOutput = true;
} else if (arg === "" && !letterFlag) { } else if (arg === "" && !letterFlag) {
nomoreflags = true; noMoreFlags = true;
} else { } else {
if (letterFlag){ if (letterFlag){
display.print(`Spinny: Unrecognized flag \`-${arg}'.`); display.print(`Spinny: Unrecognized flag \`-${arg}'.`);
@ -162,7 +162,7 @@ function parseCliArgs() {
} }
return { return {
filename: filename, filename: filename,
compile_only: compileOnly, compileOnly: compileOnly,
output: output output: output
}; };
} }
@ -181,8 +181,8 @@ class spinnyParser {
// A bunch of lines // A bunch of lines
while (this.index+1 < this.file.length) { while (this.index+1 < this.file.length) {
if (now() == "\n") { if (this.now() == "\n") {
next(); this.next();
continue continue
} }
lines.push(parseLine("F")) lines.push(parseLine("F"))
@ -200,12 +200,12 @@ class spinnyParser {
let words = [] let words = []
wordLoop: wordLoop:
while (1) { while (1) {
switch (now()) { switch (this.now()) {
case " ": case " ":
case "\t": case "\t":
// advance past whitespace till // advance past whitespace till
// next word // next word
next(); this.next();
continue continue
case "#": case "#":
parseComment(); parseComment();
@ -223,11 +223,11 @@ class spinnyParser {
break wordLoop break wordLoop
break break
case "\\": case "\\":
if (now() == "\\" && peek() == "\n") { if (this.now() == "\\" && this.peek() == "\n") {
// Line continuation // Line continuation
// Go to next line // Go to next line
next(); // skip `\\' this.next(); // skip `\\'
next(); // skip `\n' this.next(); // skip `\n'
continue continue
} }
break break
@ -242,14 +242,14 @@ class spinnyParser {
this.debugStack.push(["Comment", this.index]) this.debugStack.push(["Comment", this.index])
commentLoop: commentLoop:
while (1) { while (1) {
switch (now()) { switch (this.now()) {
case "\n": case "\n":
break commentLoop break commentLoop
case "\\": case "\\":
if (peek() == "\n") if (this.peek() == "\n")
break commentLoop break commentLoop
default: default:
next(); this.next();
} }
} }
this.debugStack.pop() this.debugStack.pop()
@ -257,13 +257,18 @@ class spinnyParser {
parseWord(inside) { parseWord(inside) {
this.debugStack.push(["Word", this.index]) this.debugStack.push(["Word", this.index])
switch (now()) { let bang = false;
if (this.now() === "!") {
bang = true;
this.next();
}
switch (this.now()) {
case "{": case "{":
return {type:"lazy",lines:parseLazy()} return {bang:bang,type:"lazy",lines:parseLazy()}
case "`": case "`":
return {type:"literal",parts:parseLiteral()} return {bang:bang,type:"literal",parts:parseLiteral()}
default: default:
return {type:"identifier",parts:parseIdentifier(inside)} return {bang:bang,type:"identifier",parts:parseIdentifier(inside)}
} }
this.debugStack.pop() this.debugStack.pop()
@ -271,9 +276,9 @@ class spinnyParser {
parseLazy() { parseLazy() {
this.debugStack.push(["Lazy", this.index]) this.debugStack.push(["Lazy", this.index])
next(); // Skip `{' this.next(); // Skip `{'
let lines = [] let lines = []
while (now() !== "}") { while (this.now() !== "}") {
lines.push(parseLine("L")) lines.push(parseLine("L"))
} }
this.debugStack.pop() this.debugStack.pop()
@ -282,10 +287,10 @@ class spinnyParser {
parseLiteral() { parseLiteral() {
this.debugStack.push(["Literal", this.index]) this.debugStack.push(["Literal", this.index])
next(); // Skip `\`' this.next(); // Skip `\`'
let parts = [] let parts = [""]
while (now() !== "\'") { while (this.now() !== "\'") {
switch (now()) { switch (this.now()) {
case "\\": case "\\":
parts[parts.length-1]+=parseEscape(); parts[parts.length-1]+=parseEscape();
break break
@ -296,8 +301,8 @@ class spinnyParser {
parts.push("") parts.push("")
break break
default: default:
parts[parts.length-1]+=now(); parts[parts.length-1]+=this.now();
next(); this.next();
break break
} }
} }
@ -307,46 +312,50 @@ class spinnyParser {
parseEscape() { parseEscape() {
this.debugStack.push(["Literal", this.index]) this.debugStack.push(["Literal", this.index])
next(); // skip `\\' this.next(); // skip `\\'
let out = ""; let out = "";
switch (now()) { switch (this.now()) {
case "<": case "<":
next(); // skip `\<' this.next(); // skip `\<'
out = "<"; out = "<";
break break
case "x": case "x":
next(); // skip `x' this.next(); // skip `x'
out = String.fromCharCode(next()+next()); out = String.fromCharCode( parseInt(
this.next()+this.next()
,16));
break break
case "u": case "u":
next(); // skip `u' this.next(); // skip `u'
out = String.fromCharCode( out = String.fromCharCode( parseInt(
next()+next()+next()+next() this.next()+this.next()+this.next()+this.next()
,16));
); );
break break
case "U": case "U":
next(); // skip `U' this.next(); // skip `U'
out = String.fromCharCode( out = String.fromCharCode( parseInt(
next()+next()+next()+next()+ this.next()+this.next()+this.next()+this.next()+
next()+next()+next()+next() this.next()+this.next()+this.next()+this.next()
,16));
); );
break break
case "s": case "s":
next(); // skip `s' this.next(); // skip `s'
out = " "; out = " ";
break break
case "t": case "t":
next(); // skip `t' this.next(); // skip `t'
out = "\t"; out = "\t";
break break
case "n": case "n":
next(); // skip `n' this.next(); // skip `n'
out = "\n"; out = "\n";
break break
default: default:
// Just send the character: // Just send the character:
out = now(); // record the thing after the `\\' out = this.now(); // record the thing after the `\\'
next(); // skip over it this.next(); // skip over it
break break
} }
@ -356,22 +365,22 @@ class spinnyParser {
parseInterpolation() { parseInterpolation() {
this.debugStack.push(["Interpolation", this.index]) this.debugStack.push(["Interpolation", this.index])
next(); // skip `\<' this.next(); // skip `\<'
let line let line
while (now() !== ">") { while (this.now() !== ">") {
line = parseLine("I") line = parseLine("I")
} }
next(); // skip `>' this.next(); // skip `>'
this.debugStack.pop() this.debugStack.pop()
return line return line
} }
parseIdentifier(inside) { parseIdentifier(inside) {
this.debugStack.push(["Identifier", this.index]) this.debugStack.push(["Identifier", this.index])
let parts = [] let parts = [""]
idLoop: idLoop:
while (1) { while (1) {
switch (now()) { switch (this.now()) {
case "\\": case "\\":
parts[parts.length-1]+=parseEscape(); parts[parts.length-1]+=parseEscape();
continue continue
@ -392,8 +401,8 @@ class spinnyParser {
case "#": case "#":
break idLoop break idLoop
} }
parts[parts.length-1]+=now(); parts[parts.length-1]+=this.now();
next(); this.next();
} }
this.debugStack.pop(); this.debugStack.pop();
return parts return parts