0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ilasmでstack machine その20

Last updated at Posted at 2024-11-01

概要

ilasmでstack machineやってみた。
練習問題やってみた。

練習問題

俺言語を、ilasmに変換するコンパイラを書け。

方針

  • Plunkerを使う。
  • fizzbuzzをやる。

写真

image.png

サンプルコード

function putlp(d) {
  val = "";
  var n = 0;
	while (buf[ptr] != d)
	{
	  val += buf[ptr++];
	}
	val = '"' + val + '"';
}
function term() {
	f = buf[ptr++];
	factr2();
	return;
}
function term2() {
	ff = buf[ptr++];
	factr3();
	return;
}
function factr3() {
	if (buf[ptr] == '\0')
	{
		return;
	}
	t = 3;
	if (getnm4())
	{
	  return;
	}
	getvr4();
	ptr++;
	return;
}
function factr2() {
	if (buf[ptr] == '\0')
	{
		return;
	}
	t = 2;
	if (getnm3())
	{
	  return;
	}
	getvr3();
	ptr++;
	return;
}
function factr() {
	if (buf[ptr] == '\0')
	{
		val = 0;
		return;
	}
	if (getnm2())
	{
	  return;
	}
	t = 1;
	getvr2();
	ptr++;
	return;
}
function expr() {
	factr();
	if (buf[ptr] != '\0')
	{
		term();
		if (buf[ptr] != '\0')
  	{
  		term2();
  	}
	}
	ptr++;
	return;
}
function getvr4() {
	cccc = buf[ptr];
}
function getvr3() {
	ccc = buf[ptr];
}
function getvr2() {
	cc = buf[ptr];
}
function getvr() {
	c = buf[ptr];
}
function ordr() {
	getvr();
 	ptr++;
	ptr++;
	if (buf[ptr] == '"')
	{
	  t = 2;
		ptr++;
		putlp('"');
	}
	else
	{
		expr();
	}
	return;
}
function num() {
	if (48 <= (buf[ptr]).charCodeAt(0) && (buf[ptr]).charCodeAt(0) <= 57) 
		return true;
	else 
		return false;
}
function getnm4() {
	var ch;
	if (!num()) 
		return false;
	var n = 0;
	do
	{
		n *= 10;
		ch = buf[ptr++];
		n += ch.charCodeAt(0) - 48;
	} while (num());
	val3 = n;
	return true;
}
function getnm3() {
	var ch;
	if (!num()) 
		return false;
	var n = 0;
	do
	{
		n *= 10;
		ch = buf[ptr++];
		n += ch.charCodeAt(0) - 48;
	} while (num());
	val2 = n;
	return true;
}
function getnm2() {
	var ch;
	if (!num()) 
		return false;
	var n = 0;
	do
	{
		n *= 10;
		ch = buf[ptr++];
		n += ch.charCodeAt(0) - 48;
	} while (num());
	val = n;
	return true;
}
function getnm() {
	var ch;
	if (!num()) 
		return false;
	var n = 0;
	do
	{
		n *= 10;
		ch = buf[ptr++];
		n += ch.charCodeAt(0) - 48;
	} while (num());
	lin = n;
	return true;
}
var koko = document.getElementById('koko');
var out = document.getElementById('out');
var ptr;
var c;
var cc;
var ccc;
var f;
var val;
var val2;
var buf;
var t;
var lin;

function run() {
	var i;
	var str = koko.value;
	var label = {};
  var res = ".assembly extern mscorlib {}\n.assembly fizzbuzz {}\n.method static void main() {\n.entrypoint\n.locals init (int32 a, int32 b)	\n";
	var lines = str.split("\n");
	for (i = 0; i < lines.length; i++)
	{
	  var k = lines[i].indexOf(" #=");
	  if (k > -1) 
    {
      var lin0 = lines[i].substr(k + 3);
      k = lin0.indexOf("*");
	    if (k > -1) 
      {
        label[parseInt(lin0.substr(k + 1))] = 1;
      }
      else
      {
        label[parseInt(lin0)] = 1;
      }
    }
  }
	for (i = 0; i < lines.length; i++)
	{
	  ptr = 0;
	  c = null;
	  cc = null;
	  val = null;
	  val2 = null;
	  t = 0;
	  lin = 0;
	  buf = lines[i] + '\0';
	  getnm();
	  ptr++;
	  ordr();
	  if (label[parseInt(lin)] > 0)
	  {
      res += "label_" + lin + ":\n";
	  }
	  if (c == '?')
	  {
	    if (t == 1)
	    {
	      res += "\tldloc " + cc + "\n\tcall void [mscorlib] System.Console::WriteLine(int32)\n";
	    }
	    if (t == 2)
	    {
	      res += "\tldstr " + val + "\n\tcall void [mscorlib] System.Console::WriteLine(String)\n";
	    }
	  }
	  else if (c == '#')
	  {
	    if (t == 0)
	    {
	      res += "\tbr\tlabel_" + val + "\n";
	    }
	    if (t == 3)
	    {
	      if (cc== 'b')
	      {
	         res += "\tbrfalse\tlabel_" + val3 + "\n";
	      }
	      if (cc== 'a')
	      {
	         res += "\tldloc\ta\n\tldc.i4\t" + val2 + "\n\tbgt\tlabel_" + val3 + "\n";
	      }
	    }
	  }
	  else if (c == 'a')
	  {
	    if (t == 0)
	    {
	      res += "\tldc.i4\t" + val + "\n\tstloc\ta\n";
	    }
	    if (t == 2)
	    {
				if (f == "+") 
					f = "add";
	      res += "\tldloc\t" + cc + "\n\tldc.i4\t" + val2 + "\n\t" + f + "\n\tstloc\ta\n";
	    }
	  }
	  else if (c == 'b')
	  {
	    if (t == 2)
	    {
				if (f == "%") 
					f = "rem";
	      res += "\tldloc\t" + cc + "\n\tldc.i4\t" + val2 + "\n\t" + f + "\n";
	    } 
	  }
	}
	res += "label_255:\n\tret\n}";
	out.value = res;
}




投入したソース

10 a=0
20 a=a+1
30 #=a>100*255
40 b=a%15
50 #=b=0*120
60 b=a%5
70 #=b=0*140
80 b=a%3
90 #=b=0*160
100 ?=a
110 #=20
120 ?="fizzbuzz "
130 #=20
140 ?="buzz "
150 #=20
160 ?="fizz "
170 #=20
   

実行結果

.assembly extern mscorlib {}
.assembly fizzbuzz {}
.method static void main() {
.entrypoint
.locals init (int32 a, int32 b)	
	ldc.i4	0
	stloc	a
label_20:
	ldloc	a
	ldc.i4	1
	add
	stloc	a
	ldloc	a
	ldc.i4	100
	bgt	label_255
	ldloc	a
	ldc.i4	15
	rem
	brfalse	label_120
	ldloc	a
	ldc.i4	5
	rem
	brfalse	label_140
	ldloc	a
	ldc.i4	3
	rem
	brfalse	label_160
	ldloc a
	call void [mscorlib] System.Console::WriteLine(int32)
	br	label_20
label_120:
	ldstr "fizzbuzz "
	call void [mscorlib] System.Console::WriteLine(string)
	br	label_20
label_140:
	ldstr "buzz "
	call void [mscorlib] System.Console::WriteLine(string)
	br	label_20
label_160:
	ldstr "fizz "
	call void [mscorlib] System.Console::WriteLine(string)
	br	label_20
label_255:
	ret
}

成果物

検証

>ilasm fizzbuzz.il

Microsoft (R) .NET Framework IL Assembler.  Version 4.8.9105.0
Copyright (c) Microsoft Corporation.  All rights reserved.
Assembling 'fizzbuzz.il'  to EXE --> 'fizzbuzz.exe'
Source file is ANSI

Assembled global method main
Creating PE file

Emitting classes:

Emitting fields and methods:
Global  Methods: 1;

Emitting events and properties:
Global
Writing PE file
Operation completed successfully

>fizzbuzz
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
fizz
22
23
fizz
buzz
26
fizz
28
29
fizzbuzz
31
32
fizz
34
buzz
fizz
37
38
fizz
buzz
41
fizz
43
44
fizzbuzz
46
47
fizz
49
buzz
fizz
52
53
fizz
buzz
56
fizz
58
59
fizzbuzz
61
62
fizz
64
buzz
fizz
67
68
fizz
buzz
71
fizz
73
74
fizzbuzz
76
77
fizz
79
buzz
fizz
82
83
fizz
buzz
86
fizz
88
89
fizzbuzz
91
92
fizz
94
buzz
fizz
97
98
fizz
buzz

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?