Code Monkey home page Code Monkey logo

imp's Introduction

Imp (README TODO)

Imp is a statically typed and compiled scripting language with the goal of increasing programmer confidence.

Note that this project is under development and functionality can be expected to change rapidly. I'm blogging my progress here.

Example

func testUnion(param int | float[] | int[]) {
    match param as id {
        int -> {
            log(id)
            log("int")
        }
        int[] -> {
            log(id)
            log("int[]")
        }
        // Comment this case out for a MatchCoverage error
        float[] -> {
            log(id)
            log("float[]")
        }
    }
}

testUnion(4)
testUnion([4,3,2,1])
testUnion([4.0])

About

Goals

  • Fast Iteration - support rapid iteration through quick compilation and feedback
  • Familiar Syntax - a C-style syntax with curly brackets, no semicolons
  • Scripting Language Characteristics - execution begins at the first statement of the entry file
  • Statically Typed - catch errors at compile-time
  • Write Less Code - the syntax should be succinct and readable
  • Module System - import what you need from other files in the project

Roadmap

  • Grammars
  • Parser (custom Pratt parser)
  • AST
  • Type inference
    • Variable assignments from literals
    • Variable assignments from expressions
  • JVM Codegen
  • Core
    • For-each loop
    • While loop
    • Control flow
    • Structs
    • Functions
    • Union types
    • Generics
    • Pattern matching
    • Enums
  • First-class functions
    • Closures
    • Storing functions in a variable
  • Event loop
  • Debugger
  • Incremental compilation
  • REPL

Usage

Windows

imp.bat <filename>

Unix

chmod u+x imp # the first time only
./imp <filename>

Notes

To run the packaged Imp compiler JAR:

# in the root directory
java --enable-preview -jar target/imp-0.1.jar sample/main.imp

To see a CLOC visualization, https://codeflower.la/?name=imp&owner=mh15&branch=main.

imp's People

Contributors

mh15 avatar omnit3a avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

imp's Issues

Multiple returns

Add the capability for multiple returns from a single return statement. Requires iterator destructuring to work.

Structs

Decide how to implement structs.

Struct functions

Support defining functions that operate on structs in Uniform Function Call Syntax.

Here's the tentative syntax:

struct Vector {
    x float
    y float
}

func add(a Vector, b Vector) Vector {
    return new Vector(a.x + b.x, a.y + b.y)
}

func +(a Vector, b Vector) Vector {
    return add(a,b)
}

val sum = add(new Vector(4, 9), new Vector(5, 7))   // (9, 16)
val sum2 = sum.add(new Vector(1, 7))                       // (10, 23)
val sum4 = sum + sum2                                              // (19, 37)

This is the method by which Imp supports "methods" on "objects", however, it's really functions on structs. Important to note that while structs are implemented as classes currently the goal has been to treat them as value types.

Stack errors

This is just an idea, but maybe any errors generated while a program is running could have an error message and numerical error code, and that message could be placed on a stack, while the numerical error code could be printed to the console. Then, people could pop the errors off the stack when they need to, to help keep the console clear, and also to allow for debugging from specific locations in the code. Again, just an idea

Nested functions

Now that closures are implemented need to finalize the story on aliasing functions to variables.

Multiple type checking passes

To support recursive struct types, or struct types that reference other types, we need to have two passes in the type checking.

  • a registration pass, which saves all structs and function signatures
  • a validation pass, which validates the types using the list of registered types

This will enable referencing types defined further down in a file, like this:

struct Person {
    fname string
    lname string
    dob Date
}

struct Date {
    year int
    month int
    day int
}

It will also make the following possible:

struct Node {
    data string
    next Node
}

It seems that the first "registration" pass should happen during the parsing stage, and all "validation" should be moved to a new pass after the AST is generated.

Escape characters in string literals

log("words\n") should work fine but instead it gives the ANTLR error:

line 2:4 token recognition error at: '"declarations\'
line 2:19 token recognition error at: '")\r'
line 2:22 no viable alternative at input '(n\n'

For loops bug

Something about the for loops causes them not to execute for certain values.

Variable Naming with Variables

I'm not sure how possible this is, as I'm not aware if your able to do this in java, but something that I think could be useful is the ability to pass a variable as the name for another variable. For example, in pseudo-code:
string name = "intVariable";
int name.content = 10;
this would create an integer variable with the name intVariable
let me know what you think

Function ownership

Functions should always be owned by an object. With top-level function declarations, the owner of the functions should be the name of the class (the name of the imp file being compiled).

Scope

Devise a way to allow Function body blocks to access the outside scope.

Unable to initialize main class of test file

if a test file tries to print out the return value from a method directly, then the CompilerTest cannot initialize the test file. This also happens if you try to assign a variable the return value of a method directly

Add the Long data type

Without the long data type, the double data type has very few uses, as its often important to convert from double to long when writing the math library

Reverse for loops

Add the ability to do reverse for loops, such as...
for i in range(11, 0)
Currently this is not possible, as the loop is simply not run if the first argument(11) is larger than second argument(0)
I believe this could be done easily by checking if the first argument is larger than the second argument, and if it is, swapping the arguments

FizzBuzz test

While implementing FizzBuzz in imp, found a number of bugs.

  • negative integer literals do not parse
  • and and or in conditionals do not work
  • change loop to for

If statement variable name inference

Idea:
If statement could be written as if (varName > 0 && < 10 and the second condition would have varName inferred. This would make the code more closely mimic natural language, while still feeling logical. Note: The snippet provided is just an example. The code would infer a variable name whenever no variable name is not specified. The inferred variable name is always the same as the previous variable name that has been provided

Functions named "main" error

This makes perfect sense as currently the compiler runs on the JVM. Perhaps prefix user-defined functions named main with _ like we do in the standard library. So the body of an imp file would run in Java main and a user defined function named main would run in the _main function.

Error handling

  • errors emit suggestions. When multiple errors of the same code are found, the suggestion should only print once.
  • separate syntax and semantics logging

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.