source: tags/1.2.0/Target/UTargetCSharp.pas

Last change on this file was 133, checked in by chronos, 3 years ago
  • Added: Compile multiple action for compilation of code with mutliple compilers at once.
File size: 3.2 KB
Line 
1unit UTargetCSharp;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, UTarget, UBFTarget, Dialogs;
9
10type
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
24implementation
25
26{ TTargetCSharp }
27
28constructor TTargetCSharp.Create;
29begin
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}
45end;
46
47function TTargetCSharp.GetMemoryCell: string;
48begin
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 + ']';
55end;
56
57procedure TTargetCSharp.Compile;
58begin
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;
114end;
115
116procedure TTargetCSharp.Run;
117begin
118 inherited;
119 RunFromFile;
120end;
121
122end.
123
Note: See TracBrowser for help on using the repository browser.