lupyuen2 / blockly-bl602

Blockly for BL602 / BL604 and Rhai Script
https://lupyuen.github.io/articles/rhai
Apache License 2.0
2 stars 0 forks source link

Transcode Rhai to S-Expressions #1

Open schungx opened 3 years ago

schungx commented 3 years ago

Hi, maintainer of Rhai here.

I see that you're transcoding Rhai into an S-expression syntax.

Wonder how you're doing that and whether there is anything we can do to help.

lupyuen commented 3 years ago

Hi: Thank you so much for the awesome Rhai Scripting Engine :-)

I'm finishing my article that explains how I transcoded Rhai to uLisp S-Expressions...

So far everything works great :-)

schungx commented 3 years ago

You're probably the first one (that I know of) who walks the AST... is there anything that you find clumsy or hard to get around of?

I might also suggest:

const LED_GPIO = 11;

// instead of
let LED_GPIO = 11;

Using constants will propagate the number to all call sites, and you can get rid of one scope in uLisp because you can be sure the constant is not used anywhere in the script afterwards. It is kept in the script because it creates a constant entry in the scope.

lupyuen commented 3 years ago

Thanks for the suggestion! I'm targeting Rhai Scripting for learners who are new to coding microcontrollers. So I'm pondering whether we should teach them let vs const, or maybe start with let and introduce const later?

I'll publish the article to r/rhai real soon. Hope to generate some discussion over this :-)

schungx commented 3 years ago

I suppose in that case using let is one fewer keyword to confuse a new learner!

For a minimal language you might also want to disable keywords like continue, break, switch, while, do etc. and especially eval if you don't intend your users to touch them yet... since they would also need to be transcoded into uLisp...

I also opened an issue in your iot repo regarding compiling the Rhai engine for the BL602. I have seen Rhai compiled for the ST32 fitting under 64KB, so I wonder what is the issue you faced when using it for the BL602... I suppose Rhai can't be that much larger than uLisp...

lupyuen commented 3 years ago

Yep makes a lot of sense to reduce the keywords!

I might take some time and figure out why Rhai won't run on BL602. It's possible that the default FreeRTOS config on BL602 severely limits the Stack Space that I can use. (Sadly I'm no expert in FreeRTOS though)

schungx commented 3 years ago

I suppose you're building in release mode, right? Rust's debug build uses a LOT of stack space. Stack space requirements are a lot lower for release builds.

And I see that you seem to be using a stack allocator, which may be why you run out of stack space so easily. In that case, you'd want to minimize your data size; which means putting no_position as I suggested in that issue to save 32 bits from each AST node.

Finally, I'm not sure how large Dynamic is on 32-bit targets (which I assume the RISC-V is?)... Let me check it out. Maybe I'm using too large a tag data type. EDIT: Just checked. Dynamic is 8 bytes on 32-bit target with no_float and only_i32 so that's OK.

EDIT 2: Don't forget LTO. By default Rust turns OFF LTO, which matters a great deal to code size. Also Rust defaults to optimize on speed; you might want size instead.

lupyuen commented 3 years ago

Yep I built it for release mode: run.sh

I'm using a Custom RISC-V Target (because the standard RV32 targets don't support hardware floating-point): riscv32imacf-unknown-none-elf.json

Thanks for the tips, I'll try them as soon as I'm done with my article :-)

schungx commented 3 years ago

Remember to explicitly turn on LTO. It usually makes a huge difference in code size, especially now that you're not using many of the functions.