Giter Site home page Giter Site logo

dplassgit / d2lang Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 15.9 MB

D2 is a strongly-typed, statically-typed, (mostly) inferred-type compiled language.

License: MIT License

Java 67.51% Starlark 1.95% Assembly 0.45% C 0.09% D 28.52% Shell 0.97% Makefile 0.20% DTrace 0.32%
compiler language programming-language compiler-construction compiler-optimization optimizers

d2lang's Introduction

d2lang

D2 (d2lang) is a strongly-typed, statically-typed, inferred-type compiled language. Its syntax draws from C, Java and Python.

The D2 compiler currently compiles to X64 assembly language only. It uses nasm and gcc to assemble and link, respectively, to Windows executables.

There are hooks to support other architectures; the intermediate language is (mostly) target-agnostic and a 8085 backend is partially implemented.

See the overview for a more comprehensive description of the types, control structures, operators and statements in D2.

NOTE: D2 is not related in ANY way to "The D Programming Language" except by coincidence of name.

A PLASS Program

Contributing

See the contributor's guide.

Installing

The following 4 are required:

  1. bazel

  2. nasm

  3. gcc

  4. Java 11 or higher

  5. Optional: Eclipse and git bash shell (mingw64)

Running Tests

Run bazel test ... from the root directory.

Running the compiler

See docs/running.md.

Caveats

Only compiles to Intel x64. Only links against the Windows version of the gcc C Runtime Library. Can only use nasm and gcc.

There are various bugs.

Language sample

Canonical hello world:

println "Hello world"

Tower of Hanoi

// Ported from toy (http://www.graysage.com/cg/Compilers/Toy/hanoi.toy)

PEGS = ["", "left", "center", "right"]

printPeg: proc(peg: int) {
  print PEGS[peg]
}

hanoi: proc(n: int, fromPeg: int, usingPeg: int, toPeg: int) {
  if n != 0 {
    hanoi(n - 1, fromPeg, toPeg, usingPeg)
    print "Move disk from "
    printPeg(fromPeg)
    print " peg to "
    printPeg(toPeg)
    println " peg"
    hanoi(n - 1, usingPeg, fromPeg, toPeg)
  }
}

n = 5 // defines global
hanoi(n, 1, 2, 3)

See more samples

Why did I build D2?

See docs/history

d2lang's People

Contributors

dplassgit avatar vincenzo-sanzone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

gmh5225

d2lang's Issues

Strength reduction

If op is multiply and one operand is a power of 2, replace by operand+operand or operand<<1 (DONE)

Arrays

Literal constants (done), length (done), indexing (done)!

Extract loop invariants

  1. Detect loops lol
  2. Detect that an assignment is not related to anything else that changes inside the loop lol
  3. Move the assignment before the loop
  4. ???
  5. Profit!

Unroll short loops

  1. Detect loop
  2. Figure out what the loop bound (condition) is
  3. copy/paste, including the increment. use 'remap' to remap temps
  4. ???
  5. profit!

Figure out how to implement `return void`

Because we don't have semicolons, the next token after a return keyword is started to be interpreted as an expression.

The saving grace is that we can detect this in the static checker

Dead code detection

E.g.,
Any sibling statement after a "return" done
Any sibling statement after a "continue" or "break" (these are just "ifs", see below)
(Re?)Assignment of variable without use

Inline small procedures

  1. decide - say, if less than 10 OPs, and no branches
  2. inline - create temps for each actual/formal and re-map each actual to the new temp
  3. delete original
  4. ???
  5. Profit!

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.