PasToHtml

unit PasToHtml;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

const
  aRes: array [0..93] of string =
    (
     ('absolute'),
     ('abstract'),
     ('and'),
     ('array'),
     ('as'),
     ('asm'),
     ('assembler'),
     ('begin'),
     ('case'),
     ('cdecl'),
     ('class'),
     ('const'),
     ('constructor'),
     ('contains'),
     ('default'),
     ('destructor'),
     ('dispid'),
     ('dispinterface'),
     ('div'),
     ('do'),
     ('downto'),
     ('dynamic'),
     ('else'),
     ('end'),
     ('except'),
     ('export'),
     ('exports'),
     ('external'),
     ('far'),
     ('file'),
     ('finalization'),
     ('finally'),
     ('for'),
     ('forward'),
     ('function'),
     ('goto'),
     ('if'),
     ('implementation'),
     ('in'),
     ('inherited'),
     ('initialization'),
     ('inline'),
     ('interface'),
     ('is'),
     ('label'),
     ('library'),
     ('message'),
     ('mod'),
     ('near'),
     ('nil'),
     ('not'),
     ('object'),
     ('of'),
     ('on'),
     ('or'),
     ('out'),
     ('overload'),
     ('override'),
     ('package'),
     ('packed'),
     ('pascal'),
     ('private'),
     ('procedure'),
     ('program'),
     ('property'),
     ('protected'),
     ('public'),
     ('published'),
     ('raise'),
     ('record'),
     ('register'),
     ('reintroduce'),
     ('repeat'),
     ('requires'),
     ('resourcestring'),
     ('safecall'),
     ('set'),
     ('shl'),
     ('shr'),
     ('stdcall'),
     ('string'),
     ('then'),
     ('threadvar'),
     ('to'),
     ('try'),
     ('type'),
     ('unit'),
     ('until'),
     ('uses'),
     ('var'),
     ('virtual'),
     ('while'),
     ('with'),
     ('xor')
     );

  blank = ' ';

  aSpecial: array [0..22] of string =
    (
     ('#'),
     ('$'),
     ('&'),
     (#39), // aspa simples
     ('('),
     (')'),
     ('*'),
     ('+'),
     (','),
     ('-'),
     ('.'),
     ('/'),
     (':'),
     (';'),
     ('<'),
     ('='),
     ('>'),
     ('@'),
     ('['),
     (']'),
     ('^'),
     ('{'),
     ('}')
//     ('(*'),
//     ('(.'),
//     ('*)'),
//     ('.)'),
//     ('..'),
//     ('//'),
//     (':='),
//     ('<='),
//     ('>='),
//     ('<>')
     );

type
  TPasToHtmlOperation=class(TComponent)
  private
    nextIsComment,
    nextIsAsm: boolean;
    commentby: string;

    backcor: string;
    cor: string[7];

    antes: array [0..7] of string;
    depois: array [0..7] of string;
    
    FFileName: string;
    FIdentifierColor: TColor;
    FBackgroundColor: TColor;
    FCommentFont: TFont;
    FUserStyle: Boolean;
    FFontSize: Integer;
    FFontName: string;
    FReservedFont: TFont;
    FSymbolFont: TFont;
    FStringFont: TFont;
    FNumberFont: TFont;
    FAssemblerFont: TFont;
    procedure SetCommentFont(const Value: TFont);
    function changeline(str: string):string;
    procedure antes_depois(ind: byte; cor: string; bo,it,un: boolean);
    procedure changepas(var list: TStringList);
    procedure SetFontName(const Value: string);
    procedure SetFontSize(const Value: Integer);
  public
    constructor Create(AOwner:TComponent);override;
    destructor Destroy;override;
    procedure Execute(const ToFileName:string);
  published
    property FileName:string read FFileName write FFileName;
    property UserStyle:Boolean read FUserStyle write FUserStyle;
    property FontName:string read FFontName write SetFontName;
    property FontSize:Integer read FFontSize write SetFontSize;
    property BackgroundColor:TColor read FBackgroundColor write FBackgroundColor;
    property CommentFont:TFont read FCommentFont write SetCommentFont;
    property ReservedFont:TFont read FReservedFont write FReservedFont;
    property IdentifierColor:TColor read FIdentifierColor write FIdentifierColor;
    property SymbolFont:TFont read FSymbolFont write FSymbolFont;
    property StringFont:TFont read FStringFont write FStringFont;
    property NumberFont:TFont read FNumberFont write FNumberFont;
    property AssemblerFont:TFont read FAssemblerFont write FAssemblerFont;
  end;

implementation


function ColorToHtmlColor(cor: TColor):string;
begin
  result := inttohex(cor,6);
  result := '#' + result[5] + result[6] + result[3] + result[4] + result[1] + result[2];
end;

function foundinres(pal: string):boolean;
var
  g: integer;
begin
  result := false;
  for g := 0 to length(aRes) - 1 do
    if UpperCase(aRes[g]) = UpperCase(pal) then begin
      result := true;
      break;
    end;
end;

function isSpecial(ch: string):boolean;
var
  s: integer;
begin
  result := false;
  for s := 0 to length(aSpecial) - 1 do
    if ch = aSpecial[s] then begin
      result := true;
      break;
    end;
end;

function IsNumber(ch: char):boolean;
begin
  if ch in ['0'..'9'] then
    result := true
  else
    result := false;
end;

{ TPasToHtmlOperation }

procedure TPasToHtmlOperation.antes_depois(ind: byte; cor: string; bo, it,
  un: boolean);
begin
  antes[ind] := '<FONT COLOR="' + cor + '">';
  depois[ind] := '</FONT>';

  if bo then begin
    antes[ind] := '<B>' + antes[ind];
    depois[ind] := depois[ind] + '</B>';
  end;
  if it then begin
    antes[ind] := '<I>' + antes[ind];
    depois[ind] := depois[ind] + '</I>';
  end;
  if un then begin
    antes[ind] := '<U>' + antes[ind];
    depois[ind] := depois[ind] + '</U>';
  end;
end;

function TPasToHtmlOperation.changeline(str: string): string;
var
  palavra,newstr: string;
  n,p,len,x: integer;
begin
  n := 1;
  palavra := '';
  len := length(str);

  if nextIsComment then begin
    p := Pos(commentby,str);
    if p > 0 then begin
      str := copy(str,1,p + length(commentby) - 1) +
             depois[1] +
             copy(str,p + length(commentby),len);
      n := p + length(depois[1]) + length(commentby);
      nextIsComment := false;
    end
    else begin
      result := str;
      exit;
    end;
  end;

  if nextIsAsm then begin
    p := Pos('END',Uppercase(str));
    if p > 0 then begin
      str := copy(str,1,p - 1) +
             depois[7] +
             copy(str,p,len);
      n := p + length(depois[7]);
      nextIsAsm := false;
    end
    else begin
      result := str;
      exit;
    end;
  end;

  len := length(str);
  while n <= len do begin
    if ((str[n] = blank)or(isSpecial(str[n]))and(str[n] <> '$')) then begin
      palavra := '';

      if str[n] = '#'  then begin
        if (char(ord(str[n - 1]) - 32) in ['A'..'Z'])or(str[n - 1] = '_') then
          inc(n)
        else begin
          x := n + 1;
          while (str[x] = '$')or(str[x] = '^')or(IsNumber(str[x]))do begin
            if str[x] = '$' then
              if IsNumber(str[x - 1]) then
                break;

            inc(x);

            if str[x - 1] = '^' then
              if str[x + 1] <> '^' then begin
                inc(x);
                break;
              end;
          end;
          str := copy(str,1,n - 1) +
                 antes[5] +
                 copy(str,n,x - n) +
                 depois[5] +
                 copy(str,x,len);
          inc(n,length(antes[5]) + length(depois[5]) + x - n - 1);
        end;
      end
      else
      if (str[n] = '/')and(str[n + 1] = '/') then begin
        str := copy(str,1,n - 1) +
               antes[1] +
               copy(str,n,len) +
               depois[1];
        break;
      end
      else
      if str[n] = '{' then begin
        newstr := copy(str,n + 1,len);
        p := Pos('}',newstr);
        if p > 0 then begin
          str := copy(str,1,n - 1) +
                 antes[1] +
                 copy(str,n,p + 1) +
                 depois[1] +
                 copy(newstr,p + 1,length(newstr));
          inc(n,length(antes[1]) + length(depois[1]) + p);
        end
        else begin
          str := copy(str,1,n - 1) +
                 antes[1] +
                 copy(str,n,length(str));
          commentby := '}';
          nextIsComment := true;
          break;
        end;
      end
      else
      if (str[n] = '(')and(str[n + 1] = '*') then begin
        newstr := copy(str,n + 2,len);
        p := Pos('*)',newstr);
        if p > 0 then begin
          str := copy(str,1,n - 1) +
                 antes[1] +
                 copy(str,n,p + 3) +
                 depois[1] +
                 copy(newstr,p + 2,length(newstr));
          inc(n,length(antes[1]) + length(depois[1]) + p + 2);
        end
        else begin
          str := copy(str,1,n - 1) +
                 antes[1] +
                 copy(str,n,length(str));
          commentby := '*)';
          nextIsComment := true;
          break;
        end;
      end
      else
      if str[n] = #39 then begin
        newstr := copy(str,n + 1,len);
        p := Pos(#39,newstr);
        if p > 0 then begin
          str := copy(str,1,n - 1) +
                 antes[5] +
                 str[n] + copy(newstr,1,p) +
                 depois[5] +
                 copy(newstr,p + 1,length(newstr));
          inc(n,length(antes[5]) + length(depois[5]) + p);
        end
        else begin
          str := copy(str,1,n - 1) +
                 antes[5] +
                 copy(str,n,length(str)) +
                 depois[5];
          break;
        end;
      end
      else
      if IsSpecial(str[n]) then begin
          x := n + 1;
          while (IsSpecial(str[x]))and
                (str[x] <> #39)and
                ((str[x] <> '(')and(str[x + 1] <> '*'))and
                (str[x] <> '{')and
                ((str[x] <> '/')and(str[x + 1] <> '/')) do begin

            inc(x);
          end;
          str := copy(str,1,n - 1) +
                 antes[4] +
                 copy(str,n,x - n) +
                 depois[4] +
                 copy(str,x,len);
          inc(n,length(antes[4]) + length(depois[4]) + x - n - 1);
      end;
    end
    else
    if (str[n] = '$')and
       ( (str[n - 1] <> '_')and(not(char(ord(str[n - 1]) - 32) in ['A'..'Z'])) ) then begin
      x := n + 1;
      while (str[x] = '$')or(IsNumber(str[x]))or
            (char(ord(str[x]) - 32) in ['A'..'F']) do
        inc(x);
      str := copy(str,1,n - 1) +
             antes[6] +
             copy(str,n,x - n) +
             depois[6] +
             copy(str,x,len);
      inc(n,length(antes[6]) + length(depois[6]) + x - n - 1);
    end
    else
    if (IsNumber(str[n]))and
       ( (str[n - 1] <> '_')and(not(char(ord(str[n - 1]) - 32) in ['A'..'Z'])) ) then begin
      x := n + 1;
      while (str[x] = '$')or(IsNumber(str[x]))or
            (char(ord(str[x]) - 32) in ['A'..'F']) do
//        if not(char(ord(str[x - 1]) - 32) in ['A'..'F']) then
          inc(x);
      str := copy(str,1,n - 1) +
             antes[6] +
             copy(str,n,x - n) +
             depois[6] +
             copy(str,x,len);
      inc(n,length(antes[6]) + length(depois[6]) + x - n - 1);
    end
    else
    if IsNumber(str[n]) then begin
      palavra := palavra + str[n];
      x := n + 1;
      while IsNumber(str[x]) do begin
        palavra := palavra + str[x];
        inc(x);
      end;
      n := x;
    end
    else begin
      palavra := palavra + str[n];

      if (foundinres(palavra))and((n = len)or(str[n + 1] = ' ')or(IsSpecial(str[n + 1]))) then begin
        if Uppercase(palavra) = 'ASM' then begin
          newstr := copy(str,n + 1,length(str));
          p := Pos('END',Uppercase(newstr));
          if p > 0 then begin
            str := copy(str,1,n - 3) +
                   antes[2] +
                   copy(str,n - 2,3) +
                   depois[2] +
                   antes[7] +
                   copy(newstr,1,p - 1) +
                   depois[7] +
                   copy(newstr,p,length(newstr));
            inc(n,length(antes[2]) + length(depois[2]) +
                  length(antes[7]) + length(depois[7]) +
                  length(newstr) - 4);
          end
          else begin
            str := copy(str,1,n - 3) +
                   antes[2] +
                   copy(str,n - 2,3) +
                   depois[2] +
                   antes[7] +
                   copy(str,n + 1,length(str));
            nextIsAsm := true;
            break;
          end;
        end
        else begin
          p := n - length(palavra) + 1;
          str := copy(str,1,p - 1) +
                 antes[2] +
                 palavra +
                 depois[2] +
                 copy(str,p + length(palavra),len);
          inc(n,length(antes[2]) + length(depois[2]));
        end;

        palavra := '';
      end;
    end;
    len := length(str);
    inc(n);
  end;
  result := str;
end;
procedure TPasToHtmlOperation.changepas(var list: TStringList);
var
  n: integer;
begin
  for n := 0 to list.count - 1 do
    list.strings[n] := changeline(list.strings[n]);
end;

constructor TPasToHtmlOperation.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FUserStyle:=True;

  FBackgroundColor:=RGB(226,186,24);
  FIdentifierColor:=RGB(0,0,0);
  
  FCommentFont:=TFont.Create;
  FCommentFont.Color:=RGB(128,128,128);

  FReservedFont:=TFont.Create;
  FReservedFont.Color:=RGB(0,0,128);
  
  FSymbolFont:=TFont.Create;
  FSymbolFont.Color:=RGB(0,0,255);

  FStringFont:=TFont.Create;
  FStringFont.Color:=RGB(128,0,128);
  
  FNumberFont:=TFont.Create;
  FNumberFont.Color:=RGB(255,0,0);

  FAssemblerFont:=TFont.Create;
  FAssemblerFont.Color:=RGB(0,128,0);

  FontSize:=3;
  FontName:='Courier New';
end;

destructor TPasToHtmlOperation.Destroy;
begin
  FCommentFont.Free;
  FReservedFont.Free;
  FSymbolFont.Free;
  FStringFont.Free;
  FNumberFont.Free;
  FAssemblerFont.Free;
  inherited Destroy;
end;

procedure TPasToHtmlOperation.Execute(const ToFileName:string);
var
  htm,pas: TStringList;
  fontcolor:string;
  Bol: boolean;
  Ita: boolean;
  Und: boolean;
  z: integer;
begin
  if not FileExists(FFileName)then begin
    ShowMessage('文件不存在!');
    exit;
  end;

  if FUserStyle then begin
    // Background
    backcor := ColorToHtmlColor(FBackgroundColor);
    // Comment
    cor := ColorToHtmlColor(FCommentFont.Color);
    Bol := fsBold in FCommentFont.Style;
    Ita := fsItalic in FCommentFont.Style;
    Und := fsUnderline in FCommentFont.Style;
    antes_depois(1,cor,bol,ita,und);
    // Reserved
    cor := ColorToHtmlColor(FReservedFont.Color);
    Bol := fsBold in FReservedFont.Style;
    Ita := fsItalic in FReservedFont.Style;
    Und := fsUnderline in FReservedFont.Style;
    antes_depois(2,cor,bol,ita,und);
    // Identifier
    fontcolor := ColorToHtmlColor(FIdentifierColor);
    // Symbol
    cor := ColorToHtmlColor(FSymbolFont.Color);
    Bol := fsBold in FSymbolFont.Style;
    Ita := fsItalic in FSymbolFont.Style;
    Und := fsUnderline in FSymbolFont.Style;
    antes_depois(4,cor,bol,ita,und);
    // String
    cor := ColorToHtmlColor(FStringFont.Color);
    Bol := fsBold in FStringFont.Style;
    Ita := fsItalic in FStringFont.Style;
    Und := fsUnderline in FStringFont.Style;
    antes_depois(5,cor,bol,ita,und);
    // Number
    cor := ColorToHtmlColor(FNumberFont.Color);
    Bol := fsBold in FNumberFont.Style;
    Ita := fsItalic in FNumberFont.Style;
    Und := fsUnderline in FNumberFont.Style;
    antes_depois(6,cor,bol,ita,und);
    // Assembler
    cor := ColorToHtmlColor(FAssemblerFont.Color);
    Bol := fsBold in FAssemblerFont.Style;
    Ita := fsItalic in FAssemblerFont.Style;
    Und := fsUnderline in FAssemblerFont.Style;
    antes_depois(7,cor,bol,ita,und); 
  end
  else begin
    backcor := '#FFFFFF';
    fontsize := 3;
    fontcolor := '#000000';
  end;

  nextIsComment := false;
  nextIsAsm := false;

  Screen.Cursor := crHourGlass;
  htm := TStringList.Create;
  pas := TStringList.Create;
  try
    try
      htm.clear;
      pas.clear;
      pas.LoadFromFile(FFileName);
      with htm do begin
        add('<HTML>');
        add('<HEAD>');
        add('<TITLE>' + FFileName + '</TITLE>');
        add('<META NAME="GENERATOR" CONTENT="Pascal To HTML by Bruno Lovatti - ' +
            formatdatetime('dd/mm/yyyy - hh:mm:ss',now) + '">');
        add('</HEAD>');

        add('<BODY BGCOLOR="' + backcor + '">');
        add('<FONT FACE="' + FCommentFont.Name +
            '" SIZE="' + inttostr(fontsize) +
            '" COLOR="' + fontcolor + '">');
        add('<A NAME=' + uppercase(extractfilename(FFileName)) +
            '><CENTER><H3>' + uppercase(extractfilename(FFileName)) + '</H3></CENTER></A>');
        add('</FONT>');
        add('<HR>');
        add('<PRE>');
        add('<FONT FACE="' + FCommentFont.Name +
            '" SIZE="' + inttostr(FFontSize) +
            '" COLOR="' + fontcolor + '">');

        if FUserStyle then
          changepas(pas);

        for z := 0 to pas.count - 1 do
          htm.add(pas.strings[z]);

        add('</FONT>');
        add('</PRE>');
        add('</BODY>');
        add('</HTML>');
        SaveToFile(ToFileName);
      end;
    except
    end;
  finally
    htm.free;
    pas.free;
    screen.cursor := crDefault;
  end;
end;


procedure TPasToHtmlOperation.SetCommentFont(const Value: TFont);
begin
  if Assigned(Value) then
    FCommentFont.Assign(Value);
end;

procedure TPasToHtmlOperation.SetFontName(const Value: string);
begin
  FFontName := Value;
  FCommentFont.Name := Value;
  FReservedFont.Name := Value;
  FSymbolFont.Name := Value;
  FStringFont.Name := Value;
  FNumberFont.Name := Value;
  FAssemblerFont.Name := Value;
end;

procedure TPasToHtmlOperation.SetFontSize(const Value: Integer);
begin
  if Value>0 then
  begin
    FFontSize := Value;
    FCommentFont.Size := Value;
    FReservedFont.Size := Value;
    FSymbolFont.Size := Value;
    FStringFont.Size := Value;
    FNumberFont.Size := Value;
    FAssemblerFont.Size := Value;
  end;
end;

end.

内容概要:本文深入研究了基于深度强化学习(DRL)的微网储能系统控制策略。首先介绍了微网系统的组成及其特性,重点探讨了光伏发电、储能系统和负荷系统的关键组件数学模型。接着详细描述了Simulink仿真设计实现,包括微网环境模拟类(MicrogridEnv)、双重深度Q网络(Double DQN)算法的实现以及训练过程。为了验证该方法的有效性,文章还进行了对比实验,分别测试了规则策略、传统优化方法和DDQN策略的表现。实验结果显示,DDQN策略在成本节约、SOC合规率等方面明显优于其他两种方法。最后,本文提出了创新点与贡献总结,包括仿真-学习一体化框架、改进的DRL算法以及多维度验证,并展望了后续研究方向如多时间尺度优化、多能源协同、不确定性处理等。 适用人群:从事电力系统、微网技术研究的专业人士,以及对深度强化学习应用于能源领域感兴趣的科研人员和工程师。 使用场景及目标:①掌握微网储能系统的基本构成与工作原理;②理解如何利用深度强化学习优化微网储能控制策略;③学习具体的算法实现细节,包括环境搭建、DDQN算法实现和训练流程;④对比不同控制策略的效果,评估DDQN策略的优势。 其他说明:本文不仅提供了理论分析和技术实现,还展示了详细的实验验证过程,通过具体的实验数据证明了所提方法的有效性。此外,文中提及的多种改进措施和技术细节对于实际工程项目具有重要的参考价值。阅读本文有助于读者全面了解微网储能控制领域的最新进展,为相关研究和技术开发提供有益的指导。
内容概要:本文详细介绍了利用MATLAB/Simulink构建的一个24小时微电网仿真模型,涵盖了柴油机、光伏发电、风力发电和V2G(车辆到电网)四个主要组成部分。文中探讨了各个组件的工作原理及其相互之间的协作机制,特别是在应对功率波动时的表现。具体来说,柴油机作为基荷电源,通过精确的转速控制确保稳定的电力供应;光伏和风力发电则引入了随机性和不确定性因素,如天气突变和风速波动,增加了仿真的真实性;V2G部分展示了电动汽车如何根据电网需求进行智能充放电调度,尤其在应对突发情况时表现出色。此外,文章还提到了一些常见的仿真错误及解决方法,强调了参数设置的重要性。 适合人群:对微电网仿真、V2G技术和MATLAB/Simulink有一定兴趣的研究人员和技术爱好者。 使用场景及目标:适用于希望深入了解微电网内部运作机制的人士,尤其是那些想要研究不同类型能源如何协同工作的专业人士。通过本案例的学习,读者能够掌握如何构建复杂的电力系统仿真模型,并理解各种能量来源在实际应用中的行为特征。 其他说明:文中提供了大量具体的代码片段和参数配置建议,有助于读者更好地理解和复现实验结果。同时,作者分享了一些实践经验,如如何处理数据归一化、避免单位换算错误等,对于初学者非常有帮助。
内容概要:本文详细介绍了蓝桥杯嵌入式比赛的背景、赛制、硬件平台及软件环境,并着重分析了嵌入式客观题的重要性、考试范围及重点内容。蓝桥杯嵌入式比赛采用封闭、限时的比赛方式,硬件平台为STM32G431RBT6,软件环境涉及STM32CubeMX和MDK535。客观题占总分的15%,虽占比不大但每分关键,能影响最终排名和选手心态。考试范围涵盖模电、数电、单片机及STM32数据手册,具体包括放大器、逻辑门电路、寄存器配置等内容。文中通过真题示例与解析,阐述了答题技巧,如先易后难、排除法、注意细节及利用数据手册。备考建议包括选择合适的教材、官方资料和在线课程,建立知识体系,理论与实践结合,总结归纳错题,并合理规划时间。; 适合人群:对嵌入式开发感兴趣并准备参加蓝桥杯嵌入式比赛的学生或爱好者。; 使用场景及目标:①帮助参赛者了解蓝桥杯嵌入式比赛的赛制和要求;②指导参赛者掌握客观题的答题技巧;③提供详细的备考建议,帮助参赛者系统学习和复习相关知识。; 其他说明:嵌入式开发是一门实践性很强的学科,本文强调理论与实践相结合的学习方法,鼓励参赛者通过实验加深理解。同时,合理的时间规划和错题总结有助于提升学习效果。最后,文章表达了对参赛者的祝福和支持,希望他们在比赛中取得优异成绩。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值