Skip to content

n4-t3/blua-lang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

blua

Overview

Blua is a scripting language motivated by Lua. Think of a procedural language with a tiny Lua-like syntax.

Blua is an extremely simple scripting language. The language is ridiculously small, implemented it to understand compiler concepts.

Example Code

x := 0
pi := 3.141592
name := 'Blua'

-----------------------------------------------
-- Find the max value between two numbers
-----------------------------------------------
func max(a, b)
  if a > b then
    ret a
  end
  ret b
end

-----------------------------------------------
-- Compute the factorial of a number
-----------------------------------------------
func factorial(n)
  if n <= 1 then
    ret 1
  else
    ret n * factorial(n - 1)
  end
end

-----------------------------------------------
-- Start procedure
-----------------------------------------------
func main()
  i := 0
  while i <= 10 do
    print(i)  
    i := i + 1
  end
  for i := 1, 10 do
    print(factorial(i))
  end
end

main()

Key Features

  • Extremely small. The language is kept simple by design and includes just the absolute minimum needed to develop a working compiler.

  • Dynamically typed. The type of a variable in Blua is assigned at runtime based on its value. The core language offers just three primitive types: number, string, and bool.

  • BNF grammar. Blua's grammar is very simple and it is fully documented using BNF notation and railroad diagrams. Some programmers might prefer to use PEG, but decided to go with BNF given the abundant number of resources out there on the topic.

Documentation

Syntax

Blua is designed to be simple and avoid complexity. Its syntax is largely inspired by languages like Lua and Ruby.

Scripts are stored in plain text files with a .blua file extension. Blua is implementation-agnostic, which means that you can use with:

  • Ahead of time compilation (AOT)
  • Just in time compilation (JIT)
  • VM with bytecode
  • IR LLVM
  • Or you can simply choose to interpret/run statements directly from source

Comments

Line comments start with -- and end at the end of the line:

-- This is a comment

Variables

Blua is a dynamically typed language. Variables are assigned using the := operator:

score := 5
name := 'Blua'
isrunning := true

Control Flow

Conditionals:

if x > 10 then
  println "Consequence block"
else
  println "Alternative block"
end

Loops:

-- While loop
i := 1
while i <= 10 do
  println i
  i := i + 1
end

-- For loop
for i := 1, 10 do
  println i
end

-- For loop with step
for i := 1, 10, 2 do
  println i
end

Functions

func say(msg)
  print msg
end

func pow(base, exponent)
  ret base^exponent
end

Operators

Arithmetic: +, -, *, /, %, ^

Comparison: >, <, >=, <=, ==, ~=

Logical: and, or, ~ (negation)

About

A toy scripting language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published