Показать сообщение отдельно
Старый 27.09.2008, 21:32   #46
abcdef
Знающий
 
Регистрация: 16.09.2008
Сообщений: 299
Написано 71 полезных сообщений
(для 123 пользователей)
Ответ: создание MIDletPascal compiler'a

satan спасибо! за разбор исходника, сам знаю как это копаться в чужом коде, (иногда проще написать с нуля) да еще с такими до нельзя длинными переменными, но названия не решился сокращать, как в документации, чтоб понятней...
сегодня за компьютер не садился - был на дне города, погода-гадость

Администраторам форума: прошу удалить исходный текст из поста #35 т.к. он уже не актуален

____
представляю свои исходники для форматирования исходных текстов на языке Pascal (писался для разовой работы форматирования одного исходника, поэтому не пинать за мелкие огрехи форматирования... и кое-где добавляет лишние запятые ),
это некоторые принцыпы переконвертировки исходников с одного языка на другой, которые здесь прозвучали



_______PPAS1.pas_______
{parser *.pas files}
{by arT  2005}
{e-mail: [email protected]}
var
  inp, out : text;
  eof_inp  : boolean;
  s        : string;
  ch       : char;

procedure add(s : string);
begin
  writeln(out,s);  {s := s+#13#10; BlockWrite(out, s[1], Length(s));}
end;

procedure getch;
begin
  ch := #255;
  if eof_inp = false then
  begin
    read(inp,ch);          {BlockRead(inp, ch, 1);{}
    eof_inp := eof(inp);
  end;
end;

{-----}
procedure getNum;
begin
  repeat
    s := s+ch;
    getch;
  until not (ch in ['0'..'9']);
end;

procedure getHexNum;
begin
  repeat
    if ch in ['A'..'F'] then  inc(ch, (97-65));  {<- loChar}
    s := s+ch;
    getch;
  until not (ch in ['0'..'9','A'..'F','a'..'f']);
end;

procedure tok;
begin
  s := '';
  while (ch <= #32) do  getch;
  case ch of
   'A'..'Z',
   'a'..'z',
   '_'      : begin
                repeat
                  if ch in [#65..#90] then inc(ch, (97-65));
                  s := s+ch;
                  getch;
                until not (ch in ['0'..'9','_','A'..'Z','a'..'z']);
              end;
   '0'..'9' : begin
                repeat
                  getNum;
                until (ch<>'.');
              end;
   '$'      : getHexNum;
   '''','#' : begin
                repeat
                  if ch = '#' then
                  begin
                    repeat
                      s := s+ch;
                      getch;
                      if ch <> '$' then getNum
                        else getHexNum;
                    until ch <> '#';
                  end
                  else
                  begin
                    repeat
                      repeat
                        s := s+ch;
                        getch;
                      until ch = '''';
                      s := s+ch;
                      getch;
                    until ch <> '''';
                  end;
                until not (ch in ['''','#']);
              end;
   ':'      : begin
                s := ch;
                getch;
                if ch = '=' then
                begin
                  s := s+ch;
                  getch;
                end;
              end;
   '>'      : begin
                s := ch;
                getch;
                if ch = '=' then
                begin
                  s := s+ch;
                  getch;
                end;
              end;
   '<'      : begin
                s := ch;
                getch;
                if (ch = '=') or (ch = '>') then
                begin
                  s := s+ch;
                  getch;
                end;
              end;
   '.'      : begin
                s := ch;
                getch;
                if (ch = '.') or (ch = ')') then
                begin
                  s := s+ch;
                  getch;
                end;
              end;
   '('      : begin
                s := ch;
                getch;
                if (ch = '.') then
                begin
                  s := s+ch;
                  getch;
                end
                else if (ch = '*') then
                begin
                  repeat
                    while ch <> '*' do
                    begin
                      s := s+ch;
                      getch;
                      if ch = #13 then
                      begin
                        add(s);
                        s := '';
                        getch;
                        getch;
                      end;
                    end;
                    s := s+ch;
                    getch;
                  until ch = ')';
                  s := s+ch;
                  getch;
                end;
              end;
   '{'      : begin
                repeat
                  s := s+ch;
                  getch;
                  if ch = #13 then
                  begin
                    add(s);
                    s := '';
                    getch;
                    getch;
                  end;
                until ch = '}';
                s := s+ch;
                getch;
              end;
   else       begin
                s := ch;
                getch;
              end;
  end;
end;
{-----}

begin
  if paramCount <> 2 then
  begin
    writeLn('parser for PAS files'#13#$0A+
            'ver 0.0.1  by arT    e-mail: [email protected]'#13#10#10+
            'Usage:  PPAS1 <in.pas> <out.pas>');
    halt;
  end;
  s := paramStr(1);
  assign(inp, s);
  {$I-} reset(inp); {$I+}
  if IOresult = 0 then
  begin
    assign(out, ParamStr(2));
    {$I-} rewrite(out); {$I+}
    if IOresult = 0 then
    begin
      eof_inp := false;
      add('{'+s+'}');
      getch;
      repeat
        tok;
        add(s);
      until eof_inp;
      close(out);
    end
      else writeLn('Error:  can''t  create file <pas.pas>');
    close(inp);
  end
    else writeLn('Error:  file <'+s+'> not found !');
end.

_______PPAS2.pas_______
{parser "END" + ";"}
{by arT  2005}
{e-mail: [email protected]}
{поставить после каждого "END" символ ";", кроме случая "ELSE" и последнего "END"}
var
  f,f2 : text;
  s,s2 : string;
  ok   : boolean;
begin
  if paramCount<>2 then
  begin
    writeln('parse "END" + ";"'#13#$0A+
            'ver 0.0.1   by arT    e-mail: [email protected]'#13#10#10+
            'Usage:  PPAS2 <in.pas> <out.pas>');
    halt;
  end;
  assign(f,paramStr(1));
  {$I-}reset(f);{$I+}
  if IOresult<>0 then
  begin
    writeln('file <'+paramStr(1)+'> not found');
    halt;
  end;
  assign(f2,paramStr(2));
  rewrite(f2);
  ok := false;
  s2 := '';
  while not eof(f) do
  begin
    readln(f,s);
    {if ok and ((s<>'else') or (s<>'.')) then  writeln(f2,';');
    ok := (s='end');}
    if (s='end') then if (s2<>';') then writeln(f2,';');
    writeln(f2,s);
    s2 := s;
  end;
  close(f2);
  close(f);
end.

_______PPAS3.pas_______
{parser - collector}
{by arT  2005}
{e-mail: [email protected]}
const
  _vars  = 1;
  _proc  = 2;
  _prog  = 3;
var
  f1,f2 : text;
  str,s : string;
  p,t   : integer;


procedure out;
var
  i : integer;
begin
  if length(str)>0 then
  begin
    for i := 1 to p do  str := ' '+str;
    writeLn(f2,str);
  end;
  str := '';
end;


procedure add;
var
  b1,b2 : boolean;
begin
  if length(str)+length(s) > 80 then out;
  b1 := (length(str)>0) and (str[length(str)-1] in ['A'..'Z','a'..'z','0'..'9',' ']);
  b2 := (length(s)>0) and (s[1] in ['A'..'Z','a'..'z','0'..'9',' ','{']);
  if not(b1 and b2) then str := copy(str,1,length(str)-1); {remove spase in the end}
  str := str+s+' ';
end;


procedure pars;
begin
  if eof(f1) then
  begin
    add;
    out;
    close(f2);
    halt;
  end;
  readln(f1,s);
  {-----------}
  if (s='label') or (s='const') or (s='type') or (s='var') then
  begin
    t := _vars;
    out;
    p := 0;
    add;
    out;
    p := 2;
  end else
  if (s='procedure') or (s='function') then
  begin
    t := _proc;
    out;
    add;
    p := 0;
  end else
  if (s='begin') then
  begin
    out;
    add;
    if t<>_prog then p:=0;
    t:=_prog;
    out;
    inc(p,2);
    repeat
      pars;
    until (s='end');
    dec(p,2);
    pars;
    out;
  end else
  if (s='end') then
  begin
    out;
    add;
  end else
  if (s='repeat') then
  begin
    out;
    add;
    out;
    inc(p,2);
    repeat
      pars;
    until s='until';
    pars;
    dec(p,2);
  end else
  if (s='until') then
  begin
    out;
    add;
  end else
  if (s='case') then
  begin
    out;
    add;
    out;
    inc(p,2);
    repeat
      pars;
    until s='end';
    dec(p,2);
    pars;
    out;
  end else
  if (s='then') then
  begin
    add;
    repeat
      pars;
    until s=';';
  end else
  if (s='else') then
  begin
    out;
    inc(p,2);
    add;
    repeat
      pars;
    until (s=';');
    dec(p,2);
  end else
  if (s='do') then
  begin
    add;
    repeat
      pars;
    until s=';';
  end else
  if (s=';') then
  begin
    add;
    out;
  end else
  if (s[1]='{') then
  begin
    add;
    out;
  end else
  begin
    add;
  end;
end;

begin
  if paramCount <> 2 then
  begin
    WriteLn('parser - collector for PAS files'#13#$0A+
            'ver 0.0.1   by arT    e-mail: [email protected]'#13#10#10+
            'Usage:  PPAS3 <in.pas> <out.pas>');
    Halt;
  end;
  assign(f1,ParamStr(1));
  {$I-} reset(f1); {$I+}
  if IOresult = 0 then
  begin
    assign(f2,ParamStr(2));
    rewrite(f2);
    p := 0;
    str := '';
    t := _vars;
    repeat
      pars;
    until false;
  end
    else writeLn('error: file  <pas.pas> not found');
end.

после компиляции всех трех программ запускать bat-файлом:
run неформатированный_файл.pas форматированный_файл.pas

_______run.bat_______
запуск 
@echo off
echo usage: RUN  parss prog.pas  format.pas
ppas1 %1 tmp1.$$$
ppas2 tmp1.$$$ tmp2.$$$
ppas3 tmp2.$$$ %2
del tmp1.$$$
del tmp2.$$$
(Offline)
 
Ответить с цитированием