nesbox / TIC-80

TIC-80 is a fantasy computer for making, playing and sharing tiny games.
https://tic80.com
MIT License
4.88k stars 472 forks source link

code chunk header flags for language (feature) #1647

Closed runlow closed 2 years ago

runlow commented 2 years ago

Everything would work the same way as current, except you could also specify the language in the chunk header (in the unused byte), rather than as a comment.

For example 0 could be Lua, 1 Javascript, 2 Moonscript, 3 Wren, 4 Fennel, 5 Squirrel, etc

In other words it would no longer be necessary to have

// script:js

...at the start of the code chunk payload but if you did that anyway it would still work like it does now

Motivation: to give non-lua languages a chance for golfing/sizecoding carts because that extra line is an extra 13 bytes Motivation: not having to write // script:js each time in all *.js files

RobLoach commented 2 years ago

I believe having it be part of the code chunk makes sense, since it's part of the code. It's not something that changes often, and it makes sharing code snippets clearer.

runlow commented 2 years ago

@RobLoach Apologies but I don't follow. What's the downside to being able to specify the language in the chunk header with an extra (currently unused) byte? It would not stop people from still being able to use the current syntax if they want to.

Not sure I understand how it changing often or not is relevant, or how it "makes sense since it's part of the code". Do you care to explain?

As an aside - did you know that file extensions (foo.js and foo.lua) are completely ignored in tic80 pro? In other words if you have // script:js at the start of the file - it ignores the fact your file is *.lua I think since the extension check is meaningless as is already - it should not check for extensions at all and just check for null bytes to see if it's an plaintext file

RobLoach commented 2 years ago

If you're looking to save code space, and are using JavaScript, just 13 bytes won't really help you. I'd recommend...

  1. Use different code banks. Since it sounds like you're using Pro, you should be able to switch code banks to give yourself a lot more space
  2. Use a bundler like https://www.npmjs.com/package/tic-bundle or https://github.com/RobLoach/tic80-js
runlow commented 2 years ago

It isn't just the golfing. Having to add // script:js each time seems pointless if the file is already foo.js. If it doesn't care for "file extensions" then it should not check for them either - in my mind, maybe I'm wrong.

joshgoebel commented 2 years ago

Lets not forget there isn't always file or a filename... snippets of code are often handed about / copied pasted, etc...

nesbox commented 2 years ago

ok @runlow how do you see the setting of this flag from the code editor?

runlow commented 2 years ago

@joshgoebel the snippets would still work the exact same way as now, if the coder doesn't add // script:foo or doesn't specify the language (by saying what it is) it's on them - to remember doing so. It's not like they usually explicitly state the language when they share code in Lua - people assume it's Lua.

@nesbox I don't think it would need to be necessarily set from the editor, as long as the "language byte" is in the chunk header it would work - everything else could behave the same way as it does now. The editor could recognise that the cart was loaded with that chunk header and therefore interpreting code in that language. If the user decided to use // script:foo it would overwrite what's in the chunk header. This could be a hidden feature (like the separate chunk id for compressed code) - possibly later added to the editor if there is need/demand.

As for how it could work if it were to be set from editor - it could be an icon that cycles between LU/JS/MS/WR/FN/SQ each time it's clicked but for now it could be hidden.

joshgoebel commented 2 years ago

If the user decided to use // script:foo it would overwrite what's in the chunk header.

This is surely a neat and easy way to do it. The only support case would seem to be someone who empties out a cartridge and tries to switch it from one language to another without the magic comment... they will be in a world of hurt... but I think I could agree that seems a pretty narrow edge case so that's not a great argument against.

But this request itself also seems a very narrow edge case... [without providing any UI] we're suggesting adding a very hidden feature, just for code-golfers, that is accessed by:

I think my vote would be this is a lot of effort for very little payoff, but ultimately it's up to nesbox. :-)

nesbox commented 2 years ago

OK, thank you for the ideas...

nesbox commented 2 years ago

Added CHUNK_LANG to the cart and ID to define a language

CHUNK_LANG (1 byte):
10 - Lua
11 - MRuby
12 - Js
13 - Moon
14 - Fennel
15 - Squirrel
16 - Wren
17 - Wasm

10d852a8446bd306eed375eaa64e7ee501bde435

joshgoebel commented 2 years ago

How does one set lang?

nesbox commented 2 years ago

Using an external tool, or you can save it as a project.lua or whatever and add LANG tag manually, then save as a .tic

-- <LANG>
-- 000:0a
-- </LANG>
joshgoebel commented 2 years ago

AHHHHH, I missed that because it's so small a patch because it's using the existing binary section logic. :-) Nice.