1 | unit UTargetCSharp;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, UTarget, UBFTarget, Dialogs;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TTargetCSharp }
|
---|
13 |
|
---|
14 | TTargetCSharp = class(TBFTarget)
|
---|
15 | private
|
---|
16 | function GetMemoryCell: string;
|
---|
17 | public
|
---|
18 | constructor Create; override;
|
---|
19 | procedure Compile; override;
|
---|
20 | procedure Run; override;
|
---|
21 | end;
|
---|
22 |
|
---|
23 |
|
---|
24 | implementation
|
---|
25 |
|
---|
26 | { TTargetCSharp }
|
---|
27 |
|
---|
28 | constructor TTargetCSharp.Create;
|
---|
29 | begin
|
---|
30 | inherited;
|
---|
31 | FName := 'C#';
|
---|
32 | FSourceExtension := '.cs';
|
---|
33 | FCompiledExtension := '.exe';
|
---|
34 | FRunExtension := '';
|
---|
35 | FImageIndex := 27;
|
---|
36 | FCapabilities := [tcCompile, tcRun];
|
---|
37 | {$IFDEF WINDOWS}
|
---|
38 | CompilerPath := 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\csc.exe';
|
---|
39 | ExecutorPath := '';
|
---|
40 | {$ENDIF}
|
---|
41 | {$IFDEF UNIX}
|
---|
42 | CompilerPath := '/usr/bin/mcs';
|
---|
43 | ExecutorPath := '';
|
---|
44 | {$ENDIF}
|
---|
45 | end;
|
---|
46 |
|
---|
47 | function TTargetCSharp.GetMemoryCell: string;
|
---|
48 | begin
|
---|
49 | Result := 'Memory[Pos';
|
---|
50 | if FProgram[FProgramIndex].RelIndex > 0 then
|
---|
51 | Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
|
---|
52 | else if FProgram[FProgramIndex].RelIndex < 0 then
|
---|
53 | Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
|
---|
54 | Result := Result + ']';
|
---|
55 | end;
|
---|
56 |
|
---|
57 | procedure TTargetCSharp.Compile;
|
---|
58 | begin
|
---|
59 | inherited;
|
---|
60 | Indent := 0;
|
---|
61 | FTargetCode := '';
|
---|
62 |
|
---|
63 | AddLine('using System;');
|
---|
64 | AddLine;
|
---|
65 | AddLine('public class ' + ProgramName);
|
---|
66 | AddLine('{');
|
---|
67 | Inc(Indent);
|
---|
68 | AddLine('public static void Main()');
|
---|
69 | AddLine('{');
|
---|
70 | Inc(Indent);
|
---|
71 | AddLine('int[] Memory;');
|
---|
72 | AddLine('int Pos;');
|
---|
73 | AddLine;
|
---|
74 | AddLine('Memory = new int[' + IntToStr(MemorySize) + '];');
|
---|
75 | AddLine('Pos = 0;');
|
---|
76 | FProgramIndex := 0;
|
---|
77 | while FProgramIndex < FProgram.Count do begin
|
---|
78 | case FProgram[FProgramIndex].Command of
|
---|
79 | cmPointerInc: AddLine('Pos += ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
80 | cmPointerDec: AddLine('Pos -= ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
81 | cmInc: AddLine(GetMemoryCell + ' += ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
82 | cmDec: AddLine(GetMemoryCell + ' -= ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
83 | cmOutput: AddLine('Console.Write((char)' + GetMemoryCell + ');');
|
---|
84 | cmInput: AddLine(GetMemoryCell + ' = (int)Console.ReadKey().KeyChar;');
|
---|
85 | cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
86 | cmMultiply: begin
|
---|
87 | if FProgram[FProgramIndex].Parameter = 1 then
|
---|
88 | AddLine(GetMemoryCell + ' += Memory[Pos];')
|
---|
89 | else
|
---|
90 | if FProgram[FProgramIndex].Parameter > 0 then
|
---|
91 | AddLine(GetMemoryCell + ' += Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';')
|
---|
92 | else
|
---|
93 | if FProgram[FProgramIndex].Parameter < 0 then
|
---|
94 | AddLine(GetMemoryCell + ' -= Memory[Pos] * ' + IntToStr(Abs(FProgram[FProgramIndex].Parameter)) + ';');
|
---|
95 | end;
|
---|
96 | cmLoopStart: begin
|
---|
97 | AddLine('while (' + GetMemoryCell + ' != 0)');
|
---|
98 | AddLine('{');
|
---|
99 | Inc(Indent);
|
---|
100 | end;
|
---|
101 | cmLoopEnd: begin
|
---|
102 | Dec(Indent);
|
---|
103 | AddLine('}');
|
---|
104 | end;
|
---|
105 | end;
|
---|
106 | Inc(FProgramIndex);
|
---|
107 | end;
|
---|
108 | Dec(Indent);
|
---|
109 | AddLine('}');
|
---|
110 | Dec(Indent);
|
---|
111 | AddLine('}');
|
---|
112 |
|
---|
113 | CompileToFile;
|
---|
114 | end;
|
---|
115 |
|
---|
116 | procedure TTargetCSharp.Run;
|
---|
117 | begin
|
---|
118 | inherited;
|
---|
119 | RunFromFile;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | end.
|
---|
123 |
|
---|