snek icon indicating copy to clipboard operation
snek copied to clipboard

REPL on the sneklang website

Open vanillajonathan opened this issue 4 years ago • 10 comments

A Snek REPL on the sneklang website.

vanillajonathan avatar Oct 19 '21 10:10 vanillajonathan

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

keith-packard avatar Dec 13 '21 21:12 keith-packard

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

drawkula avatar Dec 13 '21 21:12 drawkula

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 avatar Dec 14 '21 14:12 vanillajonathan

MicroPython has a REPL built with Unicorn.js.

https://micropython.org/unicorn/

vanillajonathan avatar Dec 19 '21 17:12 vanillajonathan

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.

mobluse avatar Jan 14 '22 18:01 mobluse

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.

keith-packard avatar Jan 15 '22 04:01 keith-packard

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

mobluse avatar Jan 16 '22 18:01 mobluse

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!

keith-packard avatar Dec 11 '22 06:12 keith-packard

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

markh2016 avatar Feb 16 '23 01:02 markh2016

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

vanillajonathan avatar Feb 16 '23 09:02 vanillajonathan