keith-packard / snek

Snek programming language for tiny systems
GNU General Public License v3.0
294 stars 30 forks source link

REPL on the sneklang website #38

Open vanillajonathan opened 3 years ago

vanillajonathan commented 3 years ago

A Snek REPL on the sneklang website.

keith-packard commented 2 years ago

This would be cool, but I fear it would be very difficult to prevent abuse and make it secure.

drawkula commented 2 years ago

Maybe the Unix binary can be adopted by TryItOnline (their source @ github)? They have some experience in jailing interpreters and compilers.

vanillajonathan commented 2 years ago

This would be cool, but I fear it would be very difficult to prevent abuse and make it secure.

Maybe this could be done with Emscripten to compile it down to WASM that executes the code client-side in the web browser?

vanillajonathan commented 2 years ago

MicroPython has a REPL built with Unicorn.js.

https://micropython.org/unicorn/

mobluse commented 2 years ago

MicroPython has a REPL built with Unicorn.js.

https://micropython.org/unicorn/

Unicorn is an emulator. If there is another emulator in JavaScript for Linux (or OS that supports what is needed for Posix Snek) that supports a CPU for which gcc and the dependencies for Snek exist, that might be an easy way to get Snek running in a browser.

I found this example of an Arduino UNO emulator: https://wokwi.com/arduino/projects/308893120796295745

Then I changed the loop() function to something that is more like a REPL:

void loop() {
  int incomingByte = 0; // for incoming serial data
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.print(incomingByte, DEC);
    Serial.print(" ");
    Serial.println((char)incomingByte);
  }
}

This is also open source: https://github.com/wokwi/avr8js

If one could make Snek into a library that could be called from an Arduino sketch then that Wokwi system could be used to get the Snek REPL running in a browser.

keith-packard commented 2 years ago

Katie Bell just gave a presentation at LCA 2022 where she compiled CPython with enscripten to wasm and ran the result in a browser. I suspect doing that for snek would be less work.

mobluse commented 2 years ago

I could not find that presentation on YouTube.

I tried to compile Snek for ia32 (x86) and upload it to a x86 emulator running Buildroot Linux https://copy.sh/v86/?profile=buildroot and https://github.com/copy/v86. It would not work because of dynamic libraries. Then I tried to compile it statically, but I could not get it to compile. I compiled it on Ubuntu 18.04.6 LTS (GNU/Linux 4.15.0-166-generic i686).

These were my changes to Makefile in ports/posix:

CFLAGS+=-DSNEK_MEM_INCLUDE_NAME -static $(OPT) -g -I. $(SNEK_CFLAGS) -Werror $(CPPFLAGS) $(CFLAGS_POSIX)

LIBS=-static-libgcc -Wl,-Bstatic -lreadline -Wl,-Bstatic -lm
keith-packard commented 1 year ago

As you can see, I hacked up the posix port to allow it to be built without readline so that it can be linked -static. If you're up for trying this again, please let us know!

markh2016 commented 1 year ago

Hi

I'm new to this and just started

Ive read through the manual but have a questions

How do I write to serial port as I would with arduino C++

How also do I address pins for lets say writing to a LCD in nibble mode , byte mode using the snek programming language Could you perhaps provide some examples I could follow

Thanking you

Mark Harrington

vanillajonathan commented 1 year ago

@markh2016 This issue is about another topic, here is the answer to your question: https://github.com/keith-packard/snek/discussions/80