Giter Site home page Giter Site logo

langium-minilogo's Introduction

MiniLogo in Langium

An example of implementing a simple DSL, MiniLogo, in Langium.

The core artifacts of this project are:

  • A Langium implementation of a variant of the MiniLogo language.
  • A demo web application for showing how Langium can run in the web.

MiniLogo is a example language for operating a 'pen' to draw on a surface. If you're familiar with python's Turtle graphics, it's similar to that.

This implementation is based on Eric Walkingshaw's grammar and semantics at OSU.

Which in turn, is based on the Logo language itself.

There is, at this time, a single example in examples/test.logo. Which demonstrates the aspects of MiniLogo:

  • Move commands to change position of the pen
  • Pen up/down commands to change the drawing state
  • Basic arithmetic expressions of literal ints, references, and binary expressions. Parenthesized and negated expressions are allowed as well
  • Common arithmetic binary functions are in infix notation: +, -, * and /
  • For loops allow a means to bind a single variable to an expr, with a second expression as an excluded upper bound, and a body of statements that are executed on each loop
  • Macros are definable, taking arguments upon calling that are bound to parameters which can be referenced in a macro's body
  • Color can be set for the lines that are drawn, affecting all subsequent 'move' comands.

Notably, there is no branching instruction present, as there are no booleans present in this language.

Building && Running Locally

The primary artifact of this project is producing a MiniLogo parser & associated generator. This is done by running the following.

# in case you changed the grammar
npm run langium:generate

# compile minilogo
npm run build

You can then reference the produced MiniLogo parser to directly process MiniLogo programs in your own projects.

If you just want to generate code from parsed MiniLogo programs, you can use the cli from the command line. You can test it like so.

# generate commands from a simple program
npm run generate:test

You can also use the cli more directly to generate either an AST or commands in JSON, whichever is of more interest. The AST will allow you to process the program as data, and the commands are pre-processed to support a simple stack-based drawing machine (a version of which is implemented in this project for demonstration).

# generate an AST
./bin/minilogo.js generate examples/simple.logo > examples/ast.json

# generate commands
./bin/minilogo.js generate-cmds examples/simple.logo > examples/ast.json

This will give you a a JSON array of drawing commands generated from the examples/simple.logo program. You can also run this on any other MiniLogo program that you'd like to test, such as examples/test.logo and examples/turtle.logo.

This output can be fed into another program to process the corresponding drawing commands, without needing to interact with a Minilogo program directly.

Building && Running the VSCode extension

In VSCode, open a terminal and run the following line.

npm run build:extension

Then, run the extension by hitting F5 (File menu -> Run -> Start debugging). Another VSCode instance will open, and you can open the examples folder to see the MiniLogo language in action.

Running in the Web

The secondary artifact of this project is a simple web application that demonstrates running Langium in the web, without a backend.

Image of Langium running standalone in the Browser

To run these examples standalone in the web, you need to first build the regular application, and then build & copy assets over for usage in browsers.

# in case you've adjusted the grammar
npm run langium:generate

# compile minilogo, copy, and construct the appropriate web assets
npm run build:web

This builds Minilogo, and sets up the libraries that you will need in public for all MiniLogo programs. You can startup an simple express app on localhost:3000 with a default program by running the following.

npm run serve

In the same interface you can also add in any MiniLogo program (that is recognized by this implementation of MiniLogo in Langium) that you would like to test, and see the results printed to the canvas on the right hand side. Once changes are made, you can click the update button at the bottom of the page to trigger redrawing the canvas using the current program.

If you're running after making a large quantity of changes, you can always run npm run clean before building to ensure you have a stable state.

There are some example MiniLogo programs in the examples folder that you can try out as well.

At a high-level, updating the canvas works by:

  • Monaco is started with a language client (LC), and is connected to an Minilogo language server (LS)
  • The document is updated, and the LC passes this new document (program) to the LS
  • the language server (Langium) receives the document
  • the document is parsed & an AST is produced, validations are performed, etc.
  • the generator is invoked to traverse and transform this AST into an array of drawing commands (includes evaluation of exprs)
  • these resulting commands, along with the AST, are returned as the result as a response to a successfully validated (processed) document
  • the front-end receives & processes these drawing instructions using a very simple stack-based machine
  • the machine drives visual updates to the canvas

This is a hyper generalization of each step, but should be sufficient to get a good idea of how this example implementation works.

Here are the results of a couple of example programs

Image of the resulting HTML page generated test.logo

Image of a turtle being drawn with MiniLogo via Langium

langium-minilogo's People

Contributors

emilkrebs avatar kaisalmen avatar lotes avatar montymxb avatar msujew avatar spoenemann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

langium-minilogo's Issues

Cannot start web ide

Dear team!

First of all I love the langium, and I think it is a really cool projects, and big thanks. Unfortunately I have some issues with this example.

I tried to start the repo locally, but I have lots of errors.

First there is no npm run build command now, but the readme contains this.

Second I cannot run locally the web editor based on the newest commit. I got error from the web: ncaught SyntaxError: The requested module './monaco-editor-wrapper/index.js' does not provide an export named 'vscode' (at setup.js:1:45).

I can start the web server based on a previous commit (ea44d9, editor immage) and that works fine.

Could you fix the repo readme, and give a step by step guide that works?

Sync up with Guides

Once the guides have been updated, we'll want to sync up this version of MiniLogo with the examples presented in the guides, to make for an easier experience for new users of Langium.

Invalid autogenerated 'symbol' regex pattern

Hi,

After executing the "langium:generate" script a new minilogo.monarch.ts is generated with invalid symbol regex pattern.

The original regex pattern found in this repo:
symbols: /-|,|\(|\)|\{|\}|\*|\/|\+|=/,

The new generated regex pattern:

symbols: /\(|\)|\*|\+|,|-|/|=|\{|\}/,

The image below shows the current error

Screenshot 2023-07-05 at 16 47 17

I'm running the same langium version, even using the package-lock.json. Langium should generate a valid regex pattern.

Add a 'Turtle' Program

More a fun one. Implement an example program in minilogo that draws an actual turtle, as an homage to 'Turtle' itself from python.

Langium MiniLogo example doesn't build:

Attempt to build the MiniLogo example failed because a static file is missing

``
Epistimis-Laptop:langium-minilogo stevehickman$ ./bin/cli generate examples/test.logo
node:fs:600
handleErrorFromBinding(ctx);
^

Error: ENOENT: no such file or directory, open './src/static/mini-logo.js'
at Object.openSync (node:fs:600:3)
at Object.readFileSync (node:fs:468:35)
at /Users/stevehickman/Documents/GitHub/langium-minilogo/out/cli/index.js:39:40
at Generator.next ()
at fulfilled (/Users/stevehickman/Documents/GitHub/langium-minilogo/out/cli/index.js:5:58) {
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: './src/static/mini-logo.js'
}
``

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.