Coder Social home page Coder Social logo

toylang's Introduction

Toy language

Example how to develop AST tree, parser and lexer

How to use

fn main() {
    println!("Hello, world!");
    let lexer = Lexer::new("2 + 2 * 2");
    let mut parser = Parser::new(lexer);
    let ast = parser.expr();
    println!("AST: {:?}", ast);

    let mut interpreter = Interpreter {
        symbol_table: std::collections::HashMap::new(),
    };

    println!("Result: {}", interpreter.eval(&ast));

    println!("=============================");
    let lexer_if = Lexer::new("if 3 < 2 then 1+2 else 1*4");
    let mut parser_if = Parser::new(lexer_if);
    let ast_if = parser_if.if_then_else();
    println!("AST: {:?}", ast_if);

    let mut interpreter_if = Interpreter {
        symbol_table: std::collections::HashMap::new(),
    };

    println!("Result: {}", interpreter_if.eval(&ast_if));

    println!("=============================");
    let lexer_print = Lexer::new("print 2+2*2");
    let mut parser_print = Parser::new(lexer_print);
    let ast_print = parser_print.expr();
    println!("AST: {:?}", ast_print);

    let mut interpreter_print = Interpreter {
        symbol_table: std::collections::HashMap::new(),
    };

    println!("Result: {}", interpreter_print.eval(&ast_print));
}

Features

Completed

✅ Mathematical operations

✅ If then else

✅ Built-in function

Partial

❓ While loop

Will be

❗ Variable assign

❗ While loop

❗ Functions typed

toylang's People

Contributors

ladroid avatar

Stargazers

Misha Kushka avatar

Watchers

 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.