[output]
MOVE #1,D0
MOVE D0,-(SP)
MOVE #2,D0
ADD (SP)+,D0
MOVE D0,-(SP)
MOVE #3,D0
ADD (SP)+,D0
LEA a(PC),A0
MOVE D0,(A0)
[input]
a=1+2+3
[source]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Cradle
{
public partial class frmCradle : Form
{
public frmCradle()
{
InitializeComponent();
}
private void btnCompile_Click(object sender, EventArgs e)
{
string input = tbxInputCmd.Text;
if (input.Length > 0)
{
Parser myparser = new Parser(input);
myparser.Assignment();
tbxOutput.Text = myparser.Output;
}
else
{
MessageBox.Show("입력 수식이 없습니다");
}
}
}
}
----------------------------------------------------------------------------------------------
using System;
using System.Text;
using System.IO;
using System.Media;
using System.Windows.Forms;
namespace Cradle
{
class Parser
{
private StringBuilder outBuffer;
private StringReader InBuffer;
private char look;
public char Look
{
get { return look; }
set { look = value; }
}
public string Output
{
get { return outBuffer.ToString(); }
set { ; }
}
// constructor
public Parser(string msg)
{
Console.WriteLine(msg);
outBuffer = new StringBuilder();
InBuffer = new StringReader(msg);
GetChar();
}
// method
protected void GetChar()
{
int b;
b = InBuffer.Read();
Look = (char)b;
}
protected void Error(string msg)
{
SystemSounds.Beep.Play();
MessageBox.Show(msg);
}
protected void Abort(string msg)
{
Error(msg);
throw new ArgumentException();
}
protected void Expected(string msg)
{
Abort(msg + " Expected");
}
protected void SkipWhite()
{
while (Look == '\t' || Look == ' ')
GetChar();
}
protected string GetNum()
{
string value = "";
if (Char.IsNumber(Look) == false)
Expected("Integer");
while (Char.IsNumber(Look))
{
value += Look;
GetChar();
}
SkipWhite();
return value;
}
protected string GetName()
{
string token = "";
if (Char.IsLetter(Look) == false)
Expected("Name");
while (Char.IsLetter(Look) || Char.IsNumber(Look))
{
token += Look;
GetChar();
}
SkipWhite();
return token;
}
protected void Emit(string msg)
{
outBuffer.Append(msg);
}
protected void EmitLn(string msg)
{
outBuffer.AppendLine(msg);
}
protected void Match(char ch)
{
if (Look == ch)
{
GetChar();
SkipWhite();
}
else
{
Expected("\'" + ch + "\'");
}
}
protected void Ident()
{
string name;
name = GetName();
if (Look == '(')
{
Match('(');
Match(')');
EmitLn("BSR " + name);
}
else
{
EmitLn("MOVE " + name + "(PC),D0");
}
}
protected void Multiply()
{
Match('*');
Factor();
EmitLn("MULS (SP)+,D0");
}
protected void Divide()
{
Match('/');
Factor();
EmitLn("MOVE (SP)+,D1");
EmitLn("DIVS D0,D1");
EmitLn("MOVE D1,D0");
}
protected void Add()
{
Match('+');
Term();
EmitLn("ADD (SP)+,D0");
}
protected void Subtract()
{
Match('-');
Term();
EmitLn("SUB (SP)+,D0");
EmitLn("NEG D0");
}
protected void Factor()
{
if (Look == '(')
{
Match('(');
Expression();
Match(')');
}
else if (Char.IsLetter(Look))
{
Ident();
}
else
{
EmitLn("MOVE #" + GetNum() + ",D0");
}
}
protected void Term()
{
Factor();
while (Look == '*' || Look == '/')
{
EmitLn("MOVE D0,-(SP)");
switch (Look)
{
case '*':
Multiply();
break;
case '/':
Divide();
break;
default:
Expected("Mulop");
break;
}
}
}
protected void Expression()
{
if (Look == '+' || Look == '-')
EmitLn("CLR D0");
else
Term();
while (Look == '+' || Look == '-')
{
EmitLn("MOVE D0,-(SP)");
switch (Look)
{
case '+':
Add();
break;
case '-':
Subtract();
break;
default:
Expected("Addop");
break;
}
}
}
public void Assignment()
{
string name;
name = GetName();
Match('=');
Expression();
EmitLn("LEA " + name + "(PC),A0");
EmitLn("MOVE D0,(A0)");
}
}
}