Skip to content
/ Mu Public
forked from w3b-net-au/Mu

A small demonstration how to use ANTLR 4's visitor feature.

Notifications You must be signed in to change notification settings

jamesbuch/Mu

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mu

A small interpreted language, using ANTLR 4. It supports most basic operators and decision and control structures, has variable scoping, and functions. Variables can have the types integer, float, string, boolean and for functions, these types as well as void.

Mu has string interpolation with the dollar sign prepended to a string, for example println($"Variable is #{varName}");

To run the demo script:

a: integer = 9

println($"Global scope a = #{a}")

func myfunction(i: integer, j: integer): void
  println("This is a function")

  a: integer = 10
  b: integer = 50
  c: integer = 0

  c = a + b + i * 2 + j * 3
  println($"The #{a} + #{b} + #{i} * 2 + #{j} * 3 = #{c}")
end

println($"Global scope a = #{a}")

foo: string = "foobarbazqux"
bar: integer = 1
baz: boolean = true

println($"a = #{a} foo = #{foo} bar = #{bar} baz = #{baz}")

myfunction(1000, 50000)

j: integer = 0

for j = 1 to 10
  println($"Hello, World j = #{j}")
next

b: integer = 0
while b < 5
  b = b + 1
  println($"The value of b is #{b}")
end

func f(a: integer, b: integer): integer
  println("In function f")
  return a + b
end

rc: integer = f(10, 20)

println($"Sum from f() = #{rc}")

rc = f(100, 1000)
println($"Sum from f() = #{rc}")

if "mail@server.com" =~ "[a-z0-9]+@[a-z0-9]+\.[a-z]{2,}" then
  println("The regex matches")
else
  println("I expected it to match but it did not")
end if

do:

git clone git://github.com/bkiers/Mu.git
mvn clean install
mvn -q exec:java

which will print the following to your console:

parsing: src/main/mu/test.mu
Global scope a = 9
Global scope a = 9
a = 9 foo = foobarbazqux bar = 1 baz = true
This is a function
The 10 + 50 + 1000 * 2 + 50000 * 3 = 152060
Hello, World j = 1
Hello, World j = 2
Hello, World j = 3
Hello, World j = 4
Hello, World j = 5
Hello, World j = 6
Hello, World j = 7
Hello, World j = 8
Hello, World j = 9
Hello, World j = 10
The value of b is 1
The value of b is 2
The value of b is 3
The value of b is 4
The value of b is 5
In function f
Sum from f() = 30
In function f
Sum from f() = 1100
The regex matches

Also see this stackoverflow Q&A.

About

A small demonstration how to use ANTLR 4's visitor feature.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 97.5%
  • ANTLR 1.9%
  • mupad 0.6%