Giter VIP home page Giter VIP logo

pascal.js's Introduction

Pascal.js

Description

Pascal.js is a Pascal compiler (Turbo Pascal 1.0-ish) implemented in JavaScript that outputs LLVM IR (intermediate representation). The IR can then compiled to native machine code (using LLVM as a backend), or compiled to JavaScript (via LLVM.js) so that it can run as a Node.js script or can run in a browser.

Pascal.js uses an LALR(1) (bottom-up lookahead left-to-right) parser generated using Jison.

Pascal.js is written in JavaScript and outputs JavaScript. This means you can write, compile and execute Pascal programs entirely in your browser. Try out the obligatory online demo of Pascal.js (supports at least Chrome and Firefox).

Why?

Take your pick:

  • It has been said: "Great programmers don't program in Pascal but they have built a compiler for it." Turbo Pascal was my first programming language (or it could have been Turtle Logo) and now I have built a compiler for it. I'm not sure where that leaves me ...

OR

  • Pascal and JavaScript exist. Sooner or later somebody was going to think of putting them together ...

OR

  • In the Spring of 2013 I took a compiler class as part of my graduate studies. The class project was to build a compiler in Java for a simplified Pascal variant (PCAT). For many years I have been wanting to ressurect some old Turbo Pascal programs I created in grade school. There were two obvious paths available to me: get the original Turbo Pascal compiler running using DOSBox, or create a Turbo Pascal compiler from scratch (inspired by my class work). I took both paths (of course!). This is the result of that second path.

Usage

Make sure that you have the llvm.js submodule checked out:

git submodule init
git submodule update

Download the node jison module and use it to build the lexer/parser module:

make parser.js

Compile to JavaScript using compile.js and run the resulting executable directly with node:

compile.js tests/ffib.pas build/ffib.js
node build/ffib.js

To build native executables, pascal.js uses the llc executable that is part of clang/LLVM. You can install clang on Debian/Ubuntu like this:

sudo apt-get install clang

Compile to a native executable using compile_native.js and run the resulting executable:

compile_native.js tests/ffib.pas build/ffib
build/ffib

Generate LLVM IR (skipping assembly) using ir.js like this:

node ir.js tests/ffib.pas > build/ffib.ll

Language Features Supported

The following features of Turbo Pascal 1.0 are supported:

  • Procedures and functions with nested lexical scope
  • Scalar Types: Integer, Real, Character, Byte, Boolean, Enumeration, Subrange
  • Structured Types: String, Array (multidimensional), Record, Enumeration
  • User defined types
  • Automatic and explicit type transfer (coercion): Integer(), Char()
  • Forward references (for corecursion)
  • Control statements: if/else, for, while, repeat, case
  • Standard routines: Concat, Chr, Halt, Odd, Ord, Random, Read, ReadLn, Write, WriteLn
  • Crt routine (native and Node.js only): ClrScr, Delay, GotoXY, KeyPressed, ReadKey

Missing Language Features / Caveats

The following features of Turbo Pascal 1.0 are not yet supported:

  • Pointer type
  • Set type
  • With statement
  • Typed constants
  • File types / file routines
  • Untyped variable parameters
  • Additional standard/crt routines

Some other differences from Turbo Pascal 1.0:

  • Crt unit must be explicityly specified
  • Default case branch is specified with "otherwise" rather than "else" (to address a parsing ambiguity with if/else).
  • Strings are current C-style (null terminated). Also, string assignment and concatentation leaks memory.

Intentionally Omitted Language Features

  • Labels / Goto
  • Compiler directives
  • Mem and Port built-in arrays
  • Variant Records

pascal.js's People

Contributors

kanaka avatar vidstige avatar

Stargazers

dnlkk avatar Oskar avatar wmingjian avatar Daniel Law avatar Hatem Hosny avatar vedro-compota avatar Dean H Smith avatar Sergiy Tytarenko avatar Camilo avatar  avatar Ramil Amerzyanov avatar Nikita Osipov avatar  avatar Levi Rizki Saputra avatar Nightwolf avatar Ajinkya Kulkarni avatar Dongsu Jang avatar  avatar María V. avatar  avatar Leandro Favero avatar Ahmad avatar  avatar Gergely Várhelyi-Tóth avatar Popa Marius Adrian avatar Ke Wang avatar Miao Yuyang avatar Đỗ Trọng Thư avatar David Rieger avatar lucas cordeiro da Silva avatar Anton Nikiforov avatar Alexander Simakhin avatar Rolf Lindén avatar Dumitru Uzun avatar Ilya avatar  avatar  avatar Adrian Praja avatar Aleksander Ryzhkov avatar tom zhou avatar mzsk avatar Matti Vesa avatar Michael Anthony avatar

Watchers

 avatar Julian Gamble avatar Dumitru Uzun avatar James Cloos avatar eroskoller avatar Michael Anthony avatar Paul TOTH avatar MelkorBSD avatar  avatar

pascal.js's Issues

records?

I was looking into adding with support, but found out records are not even working. Also error messages could be better, but that's another story. :-)

I'm getting this error message, you have any ideas on what have gone wrong? Should I try to upgrade llvm.js or so?

$ make && node compile.js tests/with1.pas 
./node_modules/.bin/jison parse.jison

undefined:441
            throw '// interfailzzzzzzzzzzzzzz ' + dump(value.item) + ' ::: ' + dump(value);
            ^
// interfailzzzzzzzzzzzzzz  ::: // {
//   "text": ","
// }

I'm using the following to reproduce and it fails on the variable declaration.

program Sample;
type
    TVector = record
        X: Integer;
        Y: Integer;
    end;
var
  a: TVector;
begin
    WriteLn(1);
end.

Cannot build / run tests?

Hi,
I would like to add some feature to this awesome project. However, after cloning and running make it gets stuck on this error:

pascal.js vidstige$ make
jison parse.jison
make: jison: No such file or directory
make: *** [parse.js] Error 1

I guess jison needs to be downloaded somehow. I tried npm install to no avail. Doing some research right now to see if I can resolve it.

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.