source: trunk/Target/TargetCSharp.pas

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