Coder Social home page Coder Social logo

comet's Introduction

Comet build status

Comet programming language

comet programming language

  • A simple programming language aimed to be used for educational purposes, and to teach how programming language interpreters can be made.

  • Since it's made for educational purposes, all the parts that make a lexer are made from scratch lexer, parser and evaluator.

Language Example

  • The language is a C derived language, with the ability to create structs as complex types, and declare functions as First class values.
  • The language is dynamically typed, to make it easier to implement
// variable declarations
var a = 10
var c = "some string"
var arrayValue = [1, 2, 3]

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

func isBigger(a, b) {
    return a > b
}

struct Data { 
    func init(a, b, c) {
        this.a = a
        this.c = a + b
    }
    
    func doSomething() {
        return a + c 
    } 
}
// STRUCT_DECL: struct Identifier BLOCK
// BLOCK: { functions* } 

// 
// var data = new Data(a, b, c)

// data.doSomething()

// Object declaration
var object = new()
object.field = "field value"

object.method = func(a) {
    return a * 2
}

var value = object.call()

var loopConstruct = func(a, b) {
    var sum = 0
    for i = 1; i < 10; i += 1 {
        sum += i
    }
    return sum
}

var conditional = func(a, b) {
}
// a program entrypoint
func main() {
    var c = a + add(1, 4)
    // builtin functions
    println(c)
}

Grammar

  • Below are the grammar rules for the language (it can be left recursive, we will deal with it at some point)
// Parser rules
grammar Comet;

comet:
    function_declaration+ EOF
    ;

function_declaration:
    'func' IDENTIFIER'('parameterless')' '{' statements '}' # functionDeclaration
    ;

parameterless:
    parameters*
    ;

parameters:
    IDENTIFIER(','IDENTIFIER)* | expression (','expression)*
    ;

statements:
    statement*
    ;

statement:
    assignment_statement                      # assignment
    | if_statement                            # ifStatement
    | return_statement                        # returnStatement
    | for_loop                                # forLoop
    ;

assignment_statement:
    IDENTIFIER '=' expression
    ;

if_statement:
    'if' '('expression')' '{' statements '}' ('else' '{' statements '}')?
    ;

return_statement:
    'return' expression
    ;

for_loop:
    'for' '('expression')' '{' statements '}'
    ;

# struct and object declaration to be defined later
# struct_statement:
#    'struct' IDENTIFIER '{' FIELD* '}'

expression:
    function_call
    | IDENTIFIER
    | STRING 
    | NUMBER 
    | BOOL 
    | string_concat
    | math_expression
    | array_declaration
    ;

array_declaration:
    '['(expression (','expression)*)?']'

function_call:
    IDENTIFIER '('parameterless')'
    ;

string_concat:
    STRING ('+' STRING)*
    ;

math_expression:
    term (('+' | '-') term)*
    ;

term:
    factor (('*' | '/') factor)*
    ;

factor:
    IDENTIFIER
    | string
    | number
    | '('expression')'
    ;

// Lexer rules

WS : [ \t\r\n\u000C]+ -> skip;
COMMENT : '/*' .*? '*/' -> skip;
LINE_COMMENT : '//' ~[\r\n]* -> skip;
IDENTIFIER: ('_'|LETTER)(LETTER|DIGIT|'_')*;
NUMBER: ('-' | '+')? (([1-9][0-9]*) | ([0-9]));
OPERATOR: '+' | '-' | '*' | '/' | '%' | '!';
fragment DIGIT: [0-9];
BOOL: 'true' | 'false';
LETTER: [a-zA-Z];
STRING: '"' ~('\r' | '\n' | '"')* '"' ;

Tasks

These are the list of the tasks that I think should be done before having the first release:

  • Add variable declarations
  • Add conditionals
  • Add Proper scoping
  • Add support for loops
  • Add Arrays support
  • Add Comments support
  • Add Hash support
  • Add import modules support
  • Add testing framework
  • Add build system
  • Add a standard library

Contribution

  • Feel free to submit new issues, or pull requests.

comet's People

Contributors

chermehdi avatar anouard24 avatar

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.