1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

コンパイラを作る。

Last updated at Posted at 2020-09-03

#概要

コンパイラを作る。
俺言語を俺cpuのアセンブラに変換するコンパイラを作る。
下記に、ソースとコンパイル結果を載せる。

#ソース

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

#結果

  push  1
  set  a
label20:
  get  a
  push  100
  >
bye:
  jz  bye
  get  a
  push  15
  %
  jz  label120
  get  a
  push  5
  %
  jz  label140
  get  a
  push  3
  %
  jz  label160
  get  a
  push  10
  /
  push  48
  +
  out
  get  a
  push  10
  %
  push  48
  +
  out
  jmp  label100
label120:
  push  'f'
  out
  push  'i'
  out
  push  'z'
  out
  push  'z'
  out
  push  'b'
  out
  push  'u'
  out
  push  'z'
  out
  push  'z'
  out
  jmp  label100
label140:
  push  'b'
  out
  push  'u'
  out
  push  'z'
  out
  push  'z'
  out
  jmp  label100
label160:
  push  'f'
  out
  push  'i'
  out
  push  'z'
  out
  push  'z'
  out
label100:
  push  32
  out
  get  a
  push  1
  +
  set  a
  jmp  label20
i:
  0x0000

#パースの方針

最初は、1から255の数字、そしてスペース、そしてワード(a、b、c、#、?)、=が来て、ワードか数字か”となるか、演算子(+、-、・、*、=、<、>)が来てワードの繰り返しとなる。

以上。

 

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?