Skip to content

Instantly share code, notes, and snippets.

@koorchik
Last active May 15, 2016 07:04
Show Gist options
  • Save koorchik/6ea2c43a9b294b7fbca64d72c89906d1 to your computer and use it in GitHub Desktop.
Save koorchik/6ea2c43a9b294b7fbca64d72c89906d1 to your computer and use it in GitHub Desktop.
WebbyLab's mini hackaton: HAL to JS compiler

During hackaton we've created HAL(http://amazon5.hansaworld.net/) to JavaScript compiler just for fun.

Performance impovements: 2000x 😎

Original source code in HAL:

function val MULTIPLY(val i, val j)
begin
    val res;

    res = i*j;

    MULTIPLY = res;
    return
end;

function val DIVIDE(val i, val j)
begin
    val res;

    res = i/j;

    DIVIDE = res;
    return
end;

function val ADD(val i, val j)
begin
    val res;

    res = i+j;

    ADD = res;
    return
end;

function val SUBSTRACT(val i, val j)
begin
    val res;

    res = i-j;

    SUBSTRACT = res;
    return
end;

global function benchmark()
begin
	LongInt timeStart,timeStop,result;
	longint i;
	val res;

	timeStart = getCurTick();


	for(i=0;i<1000001;i=i+1)begin
		res = res + SUBSTRACT( ADD (DIVIDE( MULTIPLY(10,20),30),40),50 );
		res = res + SUBSTRACT( ADD (DIVIDE( MULTIPLY(20,30),40),50),60 );
		res = res + SUBSTRACT( ADD (DIVIDE( MULTIPLY(30,40),50),60),70 );
		res = res + SUBSTRACT( ADD (DIVIDE( MULTIPLY(30,40),50),60),70 );
	end;

	timeStop = getCurTick();


	MessageBox(0, timeStop - timeStart & " " & res);
  return;
end;

benchmark();

Compiled to JS:

const hal = require("./runtime/hal");

function MULTIPLY(i, j) {
  let MULTIPLY;
  let res = 0;
  res = i * j;
  MULTIPLY = res;
  return MULTIPLY;
}

function DIVIDE(i, j) {
  let DIVIDE;
  let res = 0;
  res = i / j;
  DIVIDE = res;
  return DIVIDE;
}

function ADD(i, j) {
  let ADD;
  let res = 0;
  res = i + j;
  ADD = res;
  return ADD;
}

function SUBSTRACT(i, j) {
  let SUBSTRACT;
  let res = 0;
  res = i - j;
  SUBSTRACT = res;
  return SUBSTRACT;
}

function benchmark() {
  let benchmark;
  let timeStart = 0,
      timeStop = 0,
      result = 0;
  let i = 0;
  let res = 0;
  timeStart = hal.GetCurTick();

  for (i = 0; i < 1000001; i = i + 1) {
    res = res + SUBSTRACT(ADD(DIVIDE(MULTIPLY(10, 20), 30), 40), 50);
    res = res + SUBSTRACT(ADD(DIVIDE(MULTIPLY(20, 30), 40), 50), 60);
    res = res + SUBSTRACT(ADD(DIVIDE(MULTIPLY(30, 40), 50), 60), 70);
    res = res + SUBSTRACT(ADD(DIVIDE(MULTIPLY(30, 40), 50), 60), 70);
  }

  timeStop = hal.GetCurTick();
  hal.MessageBox(0, timeStop - timeStart + "" + " " + "" + res);
  return benchmark;
}

benchmark();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment