GithubHelp home page GithubHelp logo

linuskmr / fortytwo-lang Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 314 KB

FTL is my own programming language. It is inspired by Kaleidoscope, a language introduced in an LLVM tutorial.

License: GNU Affero General Public License v3.0

Rust 99.56% Dockerfile 0.44%
rust compiler rust-lang kaleidoscope-lang programming-language llvm

fortytwo-lang's Introduction

fortytwo-lang

fortytwo-lang (FTL) is a programming language. The syntax is a mix of C and Python. It is based on the programming language Kaleidoscope from an LLVM tutorial.

The goal for FTL is to compile to LLVM IR at some point in the future.

See Future Thoughts for how the language could look like later on.

See libftl for ideas regarding the standard library of FTL.

Sourcecode Documentation

To get the documentation of the fortytwo-lang sourcecode, run the following on your local computer:

cargo doc --document-private-items --open

Reserved keywords

You may don't use these for variable names as they could get keywords in the future.

Memory: ref deref alloc del new default nil

Math: shl shr bitxor bitor bitand

Logic: bool true false and or xor not

Data structures: struct arr const char string list enum

Loops: for in of while

Useful stuff: debug print error def extern

Integer data types: int8 uint8 int16 uint16 int32 uint32 int64 uint64

Floating point number data types: float32 float64

fortytwo-lang's People

Contributors

linuskmr avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

gmh5225

fortytwo-lang's Issues

Add unary operators

Add unary operators like:

  • Double vertical bar |x|, e.g. for getting the length of an array or the positive value of a number
  • Unary minus, e.g. -5

May be added here as a primary expression:

fn parse_primary_expression(&mut self) -> miette::Result<Expression> {
match self.tokens.peek() {
Some(Token { data: TokenKind::Identifier(_), .. }) => self.parse_identifier_expression(),
Some(Token { data: TokenKind::Number(_), .. }) => Ok(Expression::Number(self.parse_number()?)),
Some(Token { data: TokenKind::OpeningParentheses, .. }) => self.parse_parentheses(),
Some(Token { data: TokenKind::If, .. }) => Ok(Expression::IfElse(Box::new(self.parse_if_expression()?))),
Some(Token { data: TokenKind::For, .. }) => Ok(Expression::ForLoop(Box::new(self.parse_for_expression()?))),
other => {

Return comment token from Lexer

In Lexer::tokenize_next_item() the comment token returned by self.read_comment() has to be returned from tokenize_next_item(). This also enables changing the return type from Option<Result<Token, FTLError>> to ParseResult<Token> and updating the logic in the next() method in the Iterator implementation of Lexer.

Use peek-nth for multiple peeks

The following two functions get parameters passed because their callers need to consume some items from the underlying iterator (the lexer) in order to be able to call the right function.

fortytwo-lang/src/parser.rs

Lines 492 to 495 in 7d35e5c

fn parse_function_call(
&mut self,
name: PositionRangeContainer<String>,
) -> ParseResult<FunctionCall> {

fortytwo-lang/src/parser.rs

Lines 390 to 393 in 7d35e5c

fn parse_variable(
&mut self,
identifier: PositionRangeContainer<String>,
) -> ParseResult<PositionRangeContainer<String>> {

That can be avoided by using peek-nth, which allows to peek multiple items in advance.

Move build, test, format to own jobs in CI

In the GitHub CI, move each of the build, test and format steps, that are currently listed as steps in the build job, to an own job, i.e. create a build, test and format job.

Static BinaryOperator HashMap

This HashMap should be static to avoid creating it for every comparison of BinaryOperator.

fortytwo-lang/src/ast.rs

Lines 167 to 173 in d1d81e1

let mut precedence = HashMap::new();
precedence.insert(BinaryOperator::Less, 10);
precedence.insert(BinaryOperator::Greater, 10);
precedence.insert(BinaryOperator::Add, 20);
precedence.insert(BinaryOperator::Subtract, 20);
precedence.insert(BinaryOperator::Multiply, 30);
precedence.insert(BinaryOperator::Divide, 30);

Maybe use lazy_static.

Add Checker for accessing variables

Currently, statements simply get complied to C code, without any checks whether variables with the specified name are defined. The task is to add a checker that validates creating, accessing and dropping variables. This means that an accessed variable must be defined beforehand and at the creation of a variable, no other variable with the same may exist. This might be even extended to checking that called functions exist and have the correct type signature.

A basic implementation can be found in this gist.

Documentation generator

Implement a parser for documentation comments and a corresponding Markdown documentation generator.

Remove PositionContainer and put position into individual structs

Remove the struct PositionContainer and the file position_container.rs and put the position information into the individual structs.

To be able to access the position from various structs, it makes sense to create a Position struct and a trait Positionable to get the information from the structs:

/// Position in the source code.
struct Position {
    offset: u64,
    len: u64,
    // Maybe also store the filename of this Position
    filename: Rc<String>  // or Path?
}

// To be able to easily convert Position into SourceSpan for error handling.
impl From<Position> for miette::SourceSpan {
    ...
}

/// All structs than store position information should implement this.
pub trait Positionable {
    pub position(&self) -> Position;
}

Add executable examples

Create executable examples in the examples/ directory. They than can be executed with cargo run --example name.

FTLError should implement std::error::Error

FTLError should implement std::error::Error. Consider using thiserror.

/// A error occurred while parsing the sourcecode.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct FTLError {
/// The kind of this error.
pub kind: FTLErrorKind,
/// An additional message.
pub msg: String,
/// The position this error occurred.
pub position: PositionRange,
}

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.