Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

163 Commits

Repository files navigation

Artlang (work in progress)

logo


Note: like the language, this readme is work in progress too

Introduction

Artlang is a programming language that compiles to java bytecode. It is object-oriented has a static weak typesystem. It is easy to understand and doesn't get in your way while coding.



Features


The main function

The main function gets called automatically when the program is started. It must be defined in the top level of the program. It can either take no parameters or a string-array, which corresponds to the commandline parameters.

fnmain(){//put code here}fnmain(args:str[]){
print args[0]}

Comments

Line comments start with // and go until the end of the line. Block comments are started with /* and end with */. It is not a syntax error to leave a block comment unterminated.


Variables

Variables can be declared as mutable (using let) or as constant (using const). The type is inferred by the compiler, but can also be given explicitly. An initializer must be present.

fnmain(){// declare a variable named x with a value of 0// the type is inferred to be intlet x = 0// Use an explicit typelet y:str = "hello"// declare a constant variable
const myConst = 4
x = 4// assign to variable x
y = true // syntax error because type is inferred to be int
myConst = 4// syntax error because myConst is a constant}

Variable Assignments

Additionally to the standard assingments using =, there are also assign-shorhands, increment/decrement operators and the walrus-assignment.

fnmain(){let x = 2
x++ // increment x by one
x-- // decrement x by one
x *= 2// multiply x by two// unlike other languages, artlang treats an// assignment as a statement, not an expression
print x = 3// error, because the type of x = 3 is void// if you want an assignment to return the assigned// value, use the walrus-operator instead
print x := 3}

Primitive Types

Primitive types are basic types used to represent e.g. numbers.

  • byte: 8-bit signed integer
  • short: 16-bit signed integer
  • int: 32-bit signed integer
  • long: 64-bit signed integer
  • float: 32-bit floating point number
  • double: 64-bit floating point number
  • bool: a value that can either be true or false
  • str: a string of characters (secretly an object, not a primitive)
fnmain(){let a:int = 2323// numbers without comma are automatically intslet b:float = 23.23// numbers with comma are automatically floats// number literals for different types123#B// byte123#S// short123#I// int (redundant)123#L// long123#F// float123#D// double// Also works for hexadecimal/binary/octal representation0xFFEE51#L// long0b1111_0011#B// byte0o711#I// int// convert between different number-typeslet myInt:int = 123let myShort:short = myInt.short// converts myInt to a short}

If & else

If/else statements can be used to control the flow of the program.

fnmain(){let num1 = 10let num2 = 20if(num1 > num2){// this code gets executed if the condition in the brackets is true
print "num1 is greater"}else{// this code gets executed if the condition is false
print "num1 is not greater"}}

The else-branch can be omitted if it is not needed. Additionaly, the curly braces can be omittet if the branch only consits of one statement.

Ifs can also be used as expressions if both an if and an else branch are present and both result in a compatible type.

fnmain(){let num1 = 10let num2 = 20
print if(num1 > num2)"num1 is greater"else"num1 is not greater"}

Loops

Artlang currently supports two loops, the while-loop and the loop-loop. while loops until a condition is false, loop loops forever. Like if-statements, curly braces are optional if the body of the loop only consists of one statement.

fnmain(){let i = 0while(i < 100){
print i
i++
}
loop print "Hello World!"// loops forever}

Block Expressions

In Artlang, code-blocks can be used as Expressions. This is done by putting an arrow => with an expression at the end of the block.

fnmain(){let x = {const a = someFn()
const b = someFn()
=> a * b + 3// this will get assigned to x}// the arrow must be the last statement in the block and cant be// contained in an if, while or similarlet y = {
=> 3// syntax error, because '=>' is not the last statement
print "hi"}// Block-expressions can be combined with if-expressions
let z = if(someCondition){// ... some code ...
=> value
}else{// ... some code ...
=> some_other_value
}}

Examples


Hello World
fnmain(){
print "Hello World!"}

FizzBuzz
fn fizzBuzz(){let i = 0while((i := i + 1) <= 100){let output = if(i % 3 == 0)"Fizz"else""if(i % 5 == 0) output += "Buzz"
if (output.equals("")) print i else print output
}}

About

Artlang is an object-oriented programming language that compiles to java bytecode. The goal of artlang is to provide an easy to understand and fast to write language.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages