Simple, C-like programming language

Related tags

Miscellaneous cup
Overview

CUP: C(ompiler) U(nder) P(rogress)

A badly named, in-progress programming language just to learn how these things work. Wait, doesn't everyone write a compiler when they're bored?

Currently, the language is comparable to C, with some syntax changes inspired by Rust (that also make it a little easier to parse). The compiler outputs assembly code in nasm format, so you will need nasm and a linker of your choise to compile it. The included Makefile and scripts use ld.

Only linux and macOS (only on x86_64) are supported.

Building

Build the compiler build/cupcc using:

make

Compile a test program to nasm using:

build/cupcc /path/to/test.cup -o test.nasm

Assemble and link the assembly to a binary:

make test.out   # converts XXX.nasm to XXX.out

Or, you can do all the above in one go, and run the exeutable with the run.sh script, which by default creates the build/output.out executable:

./run.sh /path/to/test.cup

Code Samples

Hello World

Some common functions you'll want are located in std/common.cup

import "std/common.cup";

fn main(arc: int, argv: char**): int {
    putsln("Hello, world!");
    return 0;
}

Variables

Variables are strongly typed. You can either declare them with a type, or they can be inferred if there is an initial assignment.

fn main() {
    let x: int = 5;  // Explicity define the type
    let y = 6;       // Infer the type
    let z = x + y;   // Add them, and infer the type
}

Pointers and arrays

fn main() {
    let x: int[10];  // An array of 10 ints (initializers not supported)
    let y: int* = x; // Automatically decays to a pointer when passed or assigned
    let z = y;       // type(z) == int* also works
    
    let a = x[0];    // Access the first element (`a` is an int)
    let b = *(x+1);  // Access the second element (can use pointer arithmetic)
}

File I/O

For now, the file I/O API is essentially the same as in C. You'll find a buffered file in std/file.cup, but you can also just use the raw system calls to work with file descriptors.

A simple implementation of cat is:

0) { write(0, buf, n); // Use raw system calls n = fread(f, buf, 1024); } // file closed here because of defer } }">
import "std/file.cup";

fn main(argc: int, argv: char**) {
    for (let i = 1; i < argc; ++i) {
        let f = fopen(argv[i], 'r');
        defer fclose(f);    // Close the file at the end of the block (in each iteration)

        let buf: char[1024];
        let n = fread(f, buf, 1024); // use file-specific functions
        while (n > 0) {
            write(0, buf, n); // Use raw system calls
            n = fread(f, buf, 1024);
        }
        // file closed here because of defer
    }
}

Structs / Unions / Enums

// For now, enums just generate constant values with sequential numbers.
// They aren't a "type" on their own.
enum Type {
    TypeInt,
    TypeFloat,
    TypeChar,
}

struct Variable {
    typ: int;        // Can't use `Type` here, because it's not a type
    value: union {   // Anonymous nested structures are allowed.
        as_int: int;
        as_char: char;
        as_ptr: Variable*;  // Can recursively define types.
    };
};

fn main() {
    let x: Variable; // No struct initializers yet
    x.typ = TypeInt;
    x.value.as_int = 5;
}

Want some more examples? Check out the examples directory, or the compiler directory, which contains an in-progress rewrite of the compiler in CUP!

Issues
  • Cannot build on macOS BigSur version11.6

    Cannot build on macOS BigSur version11.6

    Build Issue Environment macOS BigSur version 11.6

    Description I was trying to follow the installation guide. I can't seem to build it on macOS BigSur.

    What I did First of all, I googled this error.

    $ ./meta/bootstrap.sh
    [+] Compiling the bootstrap compiler...
    ld: dynamic main executables must link with libSystem.dylib for architecture x86_64
    

    https://stackoverflow.com/a/65570573

    In accordance with this, fixed. (meta/bootstrap.sh)

       Darwin)
            cp bootstrap/macos.yasm bootstrap/cupcc.yasm
            yasm -f macho64 -o bootstrap/cupcc.o bootstrap/cupcc.yasm
            ld -o bootstrap/cupcc bootstrap/cupcc.o -macosx_version_min 11.6 -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem
            ;;
    

    Then, I cannnot fix the next error.

    $ ./meta/bootstrap.sh
    [+] Compiling the bootstrap compiler...
    ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in func_die from bootstrap/cupcc.o. To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
    [+] Creating build/cupcc with bootstrap compiler...
    [+] /usr/local/bin/yasm -fmacho64 -o build/cupcc.o build/cupcc.yasm
    [+] /usr/bin/ld -o build/cupcc build/cupcc.o -lSystem
    ld: library not found for -lSystem
    std/common.cup:350:14: Child exited with non-zero status: (1)
    
    opened by yu1hpa 2
  • May I have the change to get the source code?

    May I have the change to get the source code?

    Thanks for your great work. I would like to get the source code which produce the bootstrap assembly code.

    Maybe you write the bootstrap code in C, Rust ..

    Thanks.

    opened by Lisprez 2
  • initial support for invoking yasm/ld anywhere in PATH

    initial support for invoking yasm/ld anywhere in PATH

    Leverages /usr/bin/env to invoke yasm/ld from PATH. With this I can build the compiler on NixOS. The way this would work in libc is you would call execvpe instead of execve, which resolve the program to one that is found in PATH. I expect the final solution for this would be to add functionality in std to find a program in PATH. Once there's a way to access the environment variables that can be implemented.

    opened by marler8997 2
  • Hello

    Hello

    First of all, i wanna thank you for this repo :) This is not a issue, but a question, since obviously you have more experience than me, and im just trying to find something.... I was searching for more repos like this, a kind of "programming language", but i literally found nothing, if you had to know some code that could help i will be thankful. ! I basically would love to have it in c/cpp, but anything can work !

    Thanks for your time, and hope you reply soon :)

    opened by Mr-CYBERPAPER 1
  • use

    use "#!/usr/bin/env bash" instead of "!#/bin/bash"

    The "#!/usr/bin/env bash" shebang header is more flexible and works with more distributions. Using /usr/bin/env means bash will be selected based on PATH. This means different versions of bash can be used for each shell environment which is difficult when using a hardcoded globally share path like /bin/bash. This also makes it work on more distributions which don't use global shared paths like NixOS.

    opened by marler8997 1
Owner
Mustafa Quraish
I like almost anything to do with computers
Mustafa Quraish
StarkScript - or the Stark programming language - is a compiled C-based programming language that aims to offer the same usability as that of JavaScript's and TypeScript's

StarkScript StarkScript - or the Stark programming language - is a compiled C-based programming language that aims to offer the same usability as that

EnderCommunity 5 May 10, 2022
Arobase is a simple programming language with a c-like syntax.

Arobase Arobase is a simple programming language with a c-like syntax. Requirements gcc gas (gnu as) ld A 64 bits linux distribution (generated assemb

Njörd 15 Jun 17, 2022
Simple, C-like programming language

CUP: C(ompiler) U(nder) P(rogress) A badly named, in-progress programming language just to learn how these things work. Wait, doesn't everyone write a

Mustafa Quraish 279 Jun 22, 2022
frost is a programming language with a focus on low-friction systems programming.

❄️ frost frost programming language About frost is a programming language with a focus on low-friction systems programming.

null 4 Nov 12, 2021
The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.

Wren is a small, fast, class-based concurrent scripting language Think Smalltalk in a Lua-sized package with a dash of Erlang and wrapped up in a fami

Wren 5.7k Jun 27, 2022
A Simple Language (PHP-Like Syntax)

SL A Simple Language (PHP-Like Syntax) Usage: SL.exe <filename> A 64-bit windows executable is provided (SL.exe) alongside some demo SL code (main.sl)

null 3 Nov 15, 2021
A perfect blend of C, Java, and Python tailored for those who desire a simple yet powerful programming language.

Fastcode A perfect blend of C, Java, and Python tailored for those who desire a simple yet powerful programming language. FastCode is a procedural/str

null 27 May 31, 2022
Simple String_View implementation for C programming language.

Simple String_View implementation for C programming language.

Tsoding 34 May 14, 2022
A simple programming language using Bison and Flex in C++.

Yu Language - yulang A toy project for creating a simple programming language using Bison and Flex in C++. interface $ ./yulang Yu Language 0.0.1 (uns

Yudha Styawan 1 Oct 27, 2021
Tobsterlang is a simple imperative programming language, written in C++ with LLVM.

tobsterlang Tobsterlang is a simple imperative programming language, written in C++ with LLVM. One of its distinct features is the fact it uses XML in

TOBSTERA 8 Nov 11, 2021
Vimb - the vim like browser is a webkit based web browser that behaves like the vimperator plugin for the firefox and usage paradigms from the great editor vim.

Vimb - the vim like browser is a webkit based web browser that behaves like the vimperator plugin for the firefox and usage paradigms from the great editor vim. The goal of vimb is to build a completely keyboard-driven, efficient and pleasurable browsing-experience.

Daniel Carl 1.1k Jun 19, 2022
A ZX Spectrum-like library built for "dos-like" by Mattias Gustavsson.

ZX-Like A ZX Spectrum-like library built for "dos-like" by Mattias Gustavsson. It allows for the creation of ZX Spectrum like screens for demos, games

Oli Wilkinson 3 Oct 20, 2021
DigiMahal is the First Project of Our Team in Sharif University of Technology for Basics of Programming That in this Code we Implemented an Online Shop like DigiKala

DigiMahal is the First Project of Our Team in Sharif University of Technology for Basics of Programming That in this Code we Implemented an Online Shop like DigiKala.

mhlatifi 3 Feb 2, 2022
Ceres is designed to be a modern and minimalistic C like language.

Ceres v0.0.1 Ceres is designed to be a modern and minimalistic C like language. For now, it will be interpreted but later on I do want to write a comp

null 9 May 18, 2022
Competitive Programming - Programming👨‍💻 Questions on BinarySearch💻, LeetCode💻, CodeChef💻, Codeforces💻,DSA 450

?? Hacktoberfest2021 ?? This repository is open to all members of the GitHub community. Any member can contribute. Contribute according to the steps g

Anupam Kumar Krishnan 116 May 12, 2022
J is an array programming language

J: From C to C++20 J is an array programming language created by Ken Iverson and Roger Hui (see image below).

Conor Hoekstra 38 Jun 14, 2022
A minimal viable programming language on top of liblgpp

This project aims to implement a minimal viable programming language on top of liblgpp. setup The project requires a C++17 compiler, CMake and liblgpp

Andreas Nilsson 73 Jun 28, 2021
Loop is an object oriented programming language

Loop Loop is an object oriented programming language. How do I build and run loop? Make sure, you installed the requirements for clang and make: Debia

Loop 24 Aug 9, 2021
C implementation of the Monkey programming language.

Development of this interpreter continues here: dannyvankooten/pepper-lang C implementation of the Monkey programming language. Bytecode compiler and

Danny van Kooten 18 May 29, 2022