Changeset 26 for branches/AS8051toC/UMainForm.pas
- Timestamp:
- Oct 15, 2012, 2:10:48 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/AS8051toC/UMainForm.pas
r25 r26 8 8 Classes, SysUtils, FileUtil, SynHighlighterCpp, SynEdit, SynMemo, 9 9 SynHighlighterAny, SynHighlighterPas, Forms, Controls, Graphics, Dialogs, 10 StdCtrls, UParser ;10 StdCtrls, UParser, UCompiler; 11 11 12 12 type … … 27 27 procedure FormShow(Sender: TObject); 28 28 private 29 Parser: TParserASM51;30 29 public 31 30 MainFile: TStringList; 32 31 MainFileOut: TStringList; 33 Output: string;34 32 Macros: TStringList; 35 function IsHexNumber(Text: string): Boolean; 36 function CheckInstructionParameter: Boolean; 37 procedure Emit(Text: string); 33 Compiler: TCompiler; 34 CompilerMacro: TCompilerMacro; 38 35 procedure Error(Text: string); 39 procedure Parse;40 procedure ParseLine;41 function ParseMacro: Boolean;42 function ParseComment: Boolean;43 function ParseMacroUsage: Boolean;44 function ParseKeywords: Boolean;45 function ParseInstruction: Boolean;46 procedure ParseIfExpression;47 procedure ParseDbExpression;48 function ParseString: string;49 36 end; 50 37 … … 71 58 begin 72 59 MainFile.LoadFromFile(EditPath.Text); 73 Parser.Source := LowerCase(MainFile.Text); 74 Parser.Position := 1; 75 Output := ''; 76 Parse; 77 MainFileOut.Text := Output; 60 MainFileOut.Text := CompilerMacro.Compile(MainFile.Text); 61 MainFileOut.Text := Compiler.Compile(MainFileOut.Text); 78 62 ForceDirectoriesUTF8(ExtractFileDir(EditPath.Text) + DirectorySeparator + 'C'); 79 63 MainFileOut.SaveToFile(ExtractFileDir(EditPath.Text) + DirectorySeparator + 'C' + … … 94 78 procedure TFormMain.FormCreate(Sender: TObject); 95 79 begin 96 Parser := TParserASM51.Create;97 Parser.OnError := Error;98 80 MainFile := TStringList.Create; 99 81 MainFileOut := TStringList.Create; 100 Macros := TStringList.Create; 82 CompilerMacro := TCompilerMacro.Create; 83 CompilerMacro.Parser.OnError := Error; 84 Compiler := TCompiler.Create; 85 Compiler.Parser.OnError := Error; 101 86 end; 102 87 103 88 procedure TFormMain.FormDestroy(Sender: TObject); 104 89 begin 105 Macros.Free;106 MainFileOut.Free;107 MainFile.Free;108 Parser.Free;90 FreeAndNil(MainFile); 91 FreeAndNil(MainFileOut); 92 FreeAndNil(CompilerMacro); 93 FreeAndNil(Compiler); 109 94 end; 110 95 … … 115 100 end; 116 101 117 function TFormMain.IsHexNumber(Text: string): Boolean;102 procedure TFormMain.Error(Text: string); 118 103 begin 119 Result := (LowerCase(Copy(Text, Length(Text), 1)) = 'h') and120 (Parser.IsHexNumber(Copy(Text, 1, Length(Text) - 1)));104 Compiler.Emit('Error: ' + Text + LineEnding); 105 Compiler.Parser.Position := Length(Compiler.Parser.Source) + 1; 121 106 end; 122 107 123 function TFormMain.CheckInstructionParameter: Boolean;124 begin125 Text := Parser.GetNext;126 Result := False;127 if Parser.IsNumber(Text) then Result := True128 else if Text = '#' then begin129 Result := True;130 Parser.Expect('#');131 if IsHexNumber(Parser.GetNext) then Emit(Parser.ReadNext)132 else if Parser.IsNumber(Parser.GetNext) then Emit(Parser.ReadNext)133 else Error('Invalid parameter');134 end135 else if Copy(Text, 1, 1) = '@' then begin136 if IsHexNumber(Copy(Text, 2, Length(Text))) then Result := True;137 Emit(Parser.ReadNext);138 end;139 end;140 108 141 procedure TFormMain.Emit(Text: string);142 begin143 Output := Output + Text;144 end;145 146 procedure TFormMain.Error(Text: string);147 begin148 Emit('Error: ' + Text + LineEnding);149 Parser.Position := Length(Parser.Source) + 1;150 end;151 152 procedure TFormMain.Parse;153 var154 I: Integer;155 Line: string;156 Part: string;157 Comment: string;158 OutLine: string;159 begin160 with MainFileOut do begin161 while not Parser.Eof do begin162 ParseLine;163 if Parser.GetNext = 'end' then Break;164 end;165 end;166 end;167 168 procedure TFormMain.ParseLine;169 var170 Part: string;171 OutLine: string;172 Instruction: string;173 Macro: string;174 Variable: string;175 begin176 with Parser do begin177 if GetNext = '$' then begin178 Expect('$');179 if GetNext = 'include' then begin180 Expect('include');181 Expect('(');182 Emit('#include ''');183 Emit(ReadNext);184 Expect('.');185 Emit('.');186 Emit(ReadNext);187 Emit('''');188 Expect(')');189 end else Error('Unknown token "' + GetNext + '"');190 end191 else if GetNext = ')' then begin192 Expect(')');193 if GetNext = 'else' then begin194 Expect('else');195 Emit('#else ');196 Expect('(');197 end else198 if GetNext = 'fi' then begin199 Expect('fi');200 Emit('#endif ');201 end; // else Error('Unknown token "' + GetNext + '"');202 end203 else if ParseComment then204 else if ParseKeywords then205 else if ParseInstruction then206 else if IsIdentificator(GetNext) then begin207 Variable := ReadNext;208 if GetNext = '%' then begin209 Expect('%');210 Variable := Variable + '##' + ReadNext;211 end;212 if GetNext = ':' then begin213 Expect(':');214 Emit(Variable + ':');215 end else216 if GetNext = 'equ' then begin217 Expect('equ');218 Emit('#define ' + Variable + ' ');219 ParseDbExpression;220 Emit(';');221 end else222 if GetNext = 'segment' then begin223 Expect('segment');224 ReadNext;225 end else Error('Unexpected token "' + Variable + '"');226 end227 else if ParseMacro then228 else if ParseMacroUsage then229 else Error('Unexpected token "' + GetNext + '"');230 Emit(LineEnding);231 end;232 end;233 234 function TFormMain.ParseMacro: Boolean;235 var236 MacroName: string;237 OldPos: Integer;238 begin239 with Parser do begin240 OldPos := Position;241 if GetNext = '%' then begin242 Result := True;243 Expect('%');244 if GetNext = 'define' then begin245 Expect('define');246 Emit('#define ');247 Expect('(');248 MacroName := ReadNext;249 Emit(MacroName);250 Expect(')');251 Emit(' ');252 Expect('(');253 while GetNext <> ')' do begin254 Emit(ReadNext);255 end;256 Expect(')');257 Emit(';');258 end else259 if GetNext = 'if' then begin260 Expect('if');261 Emit('#if ');262 Expect('(');263 ParseIfExpression;264 Expect(')');265 Expect('then');266 Expect('(');267 end else268 if GetNext = 'set' then begin269 Expect('set');270 Emit('#define ');271 Expect('(');272 Emit(ReadNext + ' ');273 Expect(',');274 ParseDbExpression;275 Expect(')');276 Emit(';');277 end else278 if GetNext = '*' then begin279 Expect('*');280 if GetNext = 'define' then begin281 Expect('define');282 Emit('#define ');283 Expect('(');284 Emit(ReadNext);285 Expect('(');286 Emit('(' + ReadNext);287 while GetNext = ',' do begin288 Expect(',');289 Emit(', ' + ReadNext);290 end;291 Expect(')');292 Emit(')');293 Expect(')');294 if GetNext = 'local' then begin295 Expect('local');296 ReadNext;297 end;298 Expect('(');299 end;300 end else begin301 Position := OldPos;302 Result := False;303 end;304 end else Result := False;305 end;306 end;307 308 function TFormMain.ParseComment: Boolean;309 begin310 with Parser do begin311 if GetNext = ';' then begin312 Result := True;313 Expect(';');314 Emit('//');315 Emit(ReadEol);316 end else Result := False;317 end;318 end;319 320 function TFormMain.ParseMacroUsage: Boolean;321 var322 MacroName: string;323 begin324 with Parser do begin325 if GetNext = '%' then begin326 Result := True;327 Expect('%');328 MacroName := ReadNext;329 Emit(MacroName);330 if GetNext = '(' then begin331 Expect('(');332 Emit('(');333 ParseDbExpression;334 //else Emit(ReadNext);335 while GetNext = ',' do begin336 Expect(',');337 Emit(',');338 ParseDbExpression;339 //if ParseMacroUsage then340 //else if GetNext <> ')' then Emit(ReadNext);341 end;342 Emit(')');343 Expect(')');344 end;345 end else Result := False;346 end;347 end;348 349 function TFormMain.ParseKeywords: Boolean;350 var351 Value: string;352 begin353 with Parser do begin354 if GetNext = 'extrn' then begin355 Expect('extrn');356 Result := True;357 ReadNext;358 Expect('(');359 ReadNext;360 while GetNext = ',' do begin361 Expect(',');362 ReadNext;363 end;364 Expect(')');365 end else366 if GetNext = 'public' then begin367 Result := True;368 Expect('public');369 ReadNext;370 while GetNext = ',' do begin371 Expect(',');372 ReadNext;373 end;374 end else375 if GetNext = 'using' then begin376 Result := True;377 Expect('using');378 Value := ReadNext;379 Emit('// using reg bank ' + Value);380 end else381 if GetNext = 'cseg' then begin382 Result := True;383 Expect('cseg');384 Expect('at');385 Value := ReadNext;386 if GetNext = 'h' then begin387 Expect('h');388 //Emit('0x' + Value);389 end; // else Emit(Value);390 end else391 if GetNext = 'rseg' then begin392 Result := True;393 Expect('rseg');394 Value := ReadNext;395 if GetNext = 'h' then begin396 Expect('h');397 //Emit('0x' + Value);398 end; // else Emit(Value);399 end else400 if GetNext = 'dbit' then begin401 Result := True;402 Expect('dbit');403 Emit('char ');404 Emit(ReadNext);405 end else406 if GetNext = 'db' then begin407 Result := True;408 Expect('db');409 Emit('char ');410 ParseDbExpression;411 while GetNext = ',' do begin412 Expect(',');413 Emit(',');414 ParseDbExpression;415 end;416 end else417 if GetNext = 'dw' then begin418 Result := True;419 Expect('dw');420 Emit('char ');421 ParseDbExpression;422 while GetNext = ',' do begin423 Expect(',');424 Emit(',');425 ParseDbExpression;426 end;427 end else428 if GetNext = 'ds' then begin429 Result := True;430 Expect('ds');431 Emit('char* ');432 ParseDbExpression;433 while GetNext = ',' do begin434 Expect(',');435 Emit(',');436 ParseDbExpression;437 end;438 end else Result := False;439 end;440 end;441 442 function TFormMain.ParseInstruction: Boolean;443 begin444 with Parser do begin445 if GetNext = 'jmp' then begin446 Expect('jmp');447 Result := True;448 Emit('goto ');449 Emit(ReadNext);450 Emit(';');451 end else452 if GetNext = 'mov' then begin453 Expect('mov');454 Result := True;455 Emit('MOV(');456 ParseDbExpression;457 Expect(',');458 Emit(',');459 ParseDbExpression;460 Emit(');');461 end else462 if GetNext = 'reti' then begin463 Result := True;464 Expect('reti');465 end else Result := False;466 end;467 end;468 469 procedure TFormMain.ParseIfExpression;470 begin471 with Parser do begin472 if ParseMacroUsage then473 else if IsNumber(GetNext) then Emit(ReadNext)474 else Error('Unknown IF parameter "' + GetNext + '"');475 if GetNext <> ')' then begin476 if GetNext = 'eq' then begin477 Expect('eq');478 Emit('= ');479 end else480 if GetNext = 'ne' then begin481 Expect('ne');482 Emit('!= ');483 end else484 if GetNext = 'gt' then begin485 Expect('gt');486 Emit('> ');487 end else488 if GetNext = 'ge' then begin489 Expect('ge');490 Emit('>= ');491 end else492 if GetNext = 'lt' then begin493 Expect('lt');494 Emit('< ');495 end else496 if GetNext = 'le' then begin497 Expect('le');498 Emit('<= ');499 end else500 if GetNext = 'or' then begin501 Expect('or');502 Emit('|| ');503 end else504 if GetNext = 'and' then begin505 Expect('and');506 Emit('&& ');507 end else508 Error('Unknown operand "' + ReadNext + '"');509 if ParseMacroUsage then510 else if IsNumber(GetNext) then Emit(ReadNext)511 else Error('Unknown IF parameter "' + GetNext + '"');512 end;513 end;514 end;515 516 procedure TFormMain.ParseDbExpression;517 begin518 with Parser do begin519 520 if ParseMacroUsage then521 else if GetNext = '$' then begin522 Expect('$');523 Emit('$');524 end525 else if GetNext = '(' then begin526 Expect('(');527 Emit('(');528 ParseDbExpression;529 Expect(')');530 Emit(')');531 end532 else if GetNext = '''' then Emit('"' + ParseString + '"')533 else if CheckInstructionParameter then534 else if IsIdentificator(GetNext) then begin535 Emit(ReadNext);536 if ParseMacroUsage then ;537 end else Error('Unknown IF parameter "' + GetNext + '"');538 539 if GetNext = '+' then begin540 Expect('+');541 Emit('+');542 ParseDbExpression;543 end else544 if GetNext = '-' then begin545 Expect('-');546 Emit('-');547 ParseDbExpression;548 end else549 if GetNext = '*' then begin550 Expect('*');551 Emit('*');552 ParseDbExpression;553 end else554 if GetNext = '/' then begin555 Expect('/');556 Emit('/');557 ParseDbExpression;558 end else559 if GetNext = 'shl' then begin560 Expect('shl');561 Emit('<< ');562 ParseDbExpression;563 end else564 if GetNext = 'shr' then begin565 Expect('shr');566 Emit('>> ');567 ParseDbExpression;568 end else569 if GetNext = 'or' then begin570 Expect('or');571 Emit('|| ');572 ParseDbExpression;573 end else574 if GetNext = 'and' then begin575 Expect('and');576 Emit('&& ');577 ParseDbExpression;578 end;579 end;580 end;581 582 function TFormMain.ParseString: string;583 begin584 with Parser do begin585 Result := '';586 Expect('''');587 while GetNext <> '''' do begin588 Result := Result + ReadNext;589 end;590 Expect('''');591 end;592 end;593 109 594 110 end.
Note:
See TracChangeset
for help on using the changeset viewer.