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