This project is read-only.

Sample scripts

Here are a few scripts for you to check out what Mirelle programming is like:

Array sorting

var source = new [7 2 8 0 9 4 6 5 1 3]

println "before: ", source

for idx in 0..(source.size - 2) do
{
  var max = source[idx]
  var maxid = idx

  for idx2 in (idx+1)..(source.size-1) do
  {
    if source[idx2] > max do
    {
      max = source[idx2]
      maxid = idx2
    }
  }
  
  if maxid != idx do
    source[idx] <=> source[maxid]
}

print "after:  ", source 

Graph drawing

use math
use colors

var chart = new chart "Leaf"
var leaf = chart.new_series "Leaf", green

var grads = 0 ~ 0.01 ~ (PI*2)
var coeff = 100
for grad in grads do
{
  var rad = (1 + sin grad) *
            (1 - 0.9 * abs sin 4*grad) *
            (0.9 + 0.05 * cos 200*grad)
            
  var x = coeff * rad * cos grad
  var y = coeff * rad * sin grad
  leaf.plot x, y
  io:wait 5
} 

Histogram example

var bars = new histogram "Graph"

var pos = 0.0
while bars.visible do
{
  var data = new [0i] * 10
  for idx in 0..9 do
  {
    var x = pos + math:PI / 10 * idx
    var y = 7 + math:sin x
    data[idx] = new complex x, y
  }
  
  bars.plot data
  
  pos += 0.1
  
  io:wait 20
} 

Simulation modelling

type test
{
  static void doit
  {
    sim:takes 25
  }
}

emit test:doit with distr:uniform 5, 10 limit 10
var result = sim:process 2
result.show
io:read 

Brainfuck interpreter

type brainfuck
{
  static int[] @cells
  static int @ptr
  static int @idx
  static string @src
  
  static void process: string src
  {
    @cells = new [0] * 30000
    @ptr = 0
    @idx = 0
    @src = src
    
    while @idx < @src.size do
    {
      var curr = @src.at @idx
      
      if curr == "+" do @cells[@ptr] += 1
        
      if curr == "-" do @cells[@ptr] -= 1
      
      if curr == ">" do @ptr += 1
      
      if curr == "<" do @ptr -= 1
      
      if curr == "." do print @cells[@ptr].char
      
      if curr == "," do @cells[@ptr] = io:read.at(0).ord
      
      if curr == "[" do scan_fwd
      
      if curr == "]" do scan_back
      
      @idx += 1
    }
  }
  
  static void scan_fwd
  {
    if @cells[@ptr] != 0 do return
    
    var braces = 0
    while braces >= 0 do
    {
      @idx += 1
      var curr = @src.at @idx
      if curr == "[" do braces += 1
      if curr == "]" do braces -= 1
    }
  }
  
  static void scan_back
  {
    var braces = 0
    while braces >= 0 do
    {
      @idx -= 1
      var curr = @src.at @idx
      if curr == "[" do braces -= 1
      if curr == "]" do braces += 1
    }
    
    @idx -= 1
  }
}

var code = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
brainfuck:process code
io:wait

Last edited Mar 15, 2011 at 10:27 AM by impworks, version 1

Comments

No comments yet.