本帖最后由 flashercs 于 2024-11-11 17:09 编辑
回复 37# TXTUSER
原文和字典的 行注释 符是 #,注释行不处理.注释行可以删掉.
原文- #行注释符#
- #SU=DC(OE+(FC+3))
- #HI=SUM(A*OE)
- #JJ=H+2
- #AD=MAX(TAN(Z),COS(X))+MAX(TAN(Z),COS(X))
- #P1=SIGNALS_USER(4567,2)
- #P2=REF(H,BARSLAST(P1))
- #AE=MAX(A,B)+MAX(C,D)+P2
-
- P1=SIGNALS_USER(4567,2)
- P2=REF(H,BARSLAST(P1))
- JJ=H+2
- JJ=H-2
- JJ=H*2
- JJ=H/2
- AD=MAX(A,B)+MAX(C,D)
复制代码 字典- #行注释符#
- #A=SIN(Y)
- #B=LOG(100)
- #C=TAN(Z)
- #D=COS(X)
- #OE=POW(3)
- #FC=SQRT(6)
- #H=TDXDLL1(4,0,1,0)
- #+=TDXDLL1(35,X,Y,0)
- #REF=TDXDLL1(5,X,Y,0)+Z
- #BARSLAST=TDXDLL1(17,X,0,0)
- #*=MULTI(A1,B1)
-
- H=TDXDLL1(4,0,1,0)
- +=TDXDLL1(6,X,Y,0)
- -=TDXDLL1(7,X,Y,0)
- *=TDXDLL1(8,X,Y,0)
- /=TDXDLL1(9,X,Y,0)
- REF=TDXDLL1(5,X,Y,0)
- BARSLAST=TDXDLL1(17,X,0,0)
- A=SIN(Y)
- B=LOG(100)
- C=TAN(Z)
- D=COS(X)
复制代码 结果- #行注释符#
- #SU=DC(OE+(FC+3))
- #HI=SUM(A*OE)
- #JJ=H+2
- #AD=MAX(TAN(Z),COS(X))+MAX(TAN(Z),COS(X))
- #P1=SIGNALS_USER(4567,2)
- #P2=REF(H,BARSLAST(P1))
- #AE=MAX(A,B)+MAX(C,D)+P2
-
- P1=SIGNALS_USER(4567,2)
- P2=TDXDLL1(5,TDXDLL1(4,0,1,0),TDXDLL1(17,SIGNALS_USER(4567,2),0,0),0)
- JJ=TDXDLL1(6,TDXDLL1(4,0,1,0),2,0)
- JJ=TDXDLL1(7,TDXDLL1(4,0,1,0),2,0)
- JJ=TDXDLL1(8,TDXDLL1(4,0,1,0),2,0)
- JJ=TDXDLL1(9,TDXDLL1(4,0,1,0),2,0)
- AD=TDXDLL1(6,MAX(SIN(Y),LOG(100)),MAX(TAN(Z),COS(X)),0)
复制代码 保存为translate.bat,编码为ANSI- <#*,:
- @echo off
- cd /d "%~dp0"
- set "batchfile=%~f0"
- Powershell -ExecutionPolicy Bypass -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::GetEncoding(0) )) )"
- pause
- exit /b
- #>
- # 注:字典和原文的注释行开头是 #
- $dicFile = "字典.txt"
- $srcFile = "原文.txt"
- $dstFile = "结果.txt"
-
- $utf8 = New-Object System.Text.UTF8Encoding -ArgumentList $false
- $ansi = [System.Text.Encoding]::GetEncoding(0)
- $op_precedence = @{
- '+' = 0
- '-' = 0
- '*' = 1
- '/' = 1
- }
- $reCell = [regex]'((?>(?>\w+)\((?>[^()]+|(?<o>\()|(?<-o>\)))*\)(?(o)(?!))|\((?>[^()]+|(?<o>\()|(?<-o>\)))*\)(?(o)(?!))|\w+)+)'
- $reParam = [regex]'(?<=[(,])((?>(?>\w+)\((?>[^()]+|(?<o>\()|(?<-o>\)))*\)(?(o)(?!))|\((?>[^()]+|(?<o>\()|(?<-o>\)))*\)(?(o)(?!))|(?>\w+)|[^,()])+)'
- $dic = @{}
- function Resolve-TokenTree {
- param([string[]]$Tokens)
- if ($null -eq $Tokens -or $Tokens.Count -eq 0) { return '' }
- if ($Tokens.Count -eq 1) { return $Tokens[0] }
- $precedence = [int]::MaxValue
- $opctr = $null
- for ($i = 0; $i -lt $Tokens.Count; ++$i) {
- if ($op_precedence.ContainsKey($Tokens[$i]) -and $op_precedence[$Tokens[$i]] -le $precedence ) {
- $opctr = $i
- $precedence = $op_precedence[$Tokens[$i]]
- }
- }
- # Sort-Object is not stable
- # $opctr = 0..($Tokens.Count - 1) | Where-Object { $op_precedence.ContainsKey($Tokens[$_]) } | Sort-Object -Property @{Expression = { $op_precedence[$Tokens[$_]] } } | Select-Object -First 1
- if ($null -eq $opctr) { return $Tokens -join '' }
- $dicms = @{
- tokens = @(
- Resolve-TokenTree $Tokens[0..($opctr - 1)]
- Resolve-TokenTree $Tokens[($opctr + 1)..($Tokens.Count - 1)]
- )
- ctr = 0
- }
- if ($dic.ContainsKey($Tokens[$opctr])) {
- $token = $reParam.Replace($dic[$Tokens[$opctr]], $evaluator2)
- } else {
- $token = $dicms.tokens[0] + $Tokens[$opctr] + $dicms.tokens[1]
- }
- return $token
- }
- function Resolve-Token {
- param([string]$Token)
- $tokens = $reCell.Split($Token) -match '\S'
- if ($tokens.Count -eq 0) { return '' }
- for ($i = 0; $i -lt $tokens.Count; $i += 2) {
- if ($tokens[$i] -match '^\w+$') {
- if ($dic.ContainsKey($tokens[$i])) {
- $tokens[$i] = Resolve-Token $dic[$tokens[$i]] # 可能变成多token
- }
- continue
- }
- if ($tokens[$i] -match '^(\w+)\((?>[^()]+|(?<o>\()|(?<-o>\)))*\)(?(o)(?!))$' -and $dic.ContainsKey($Matches[1])) {
- $func = $Matches[1]
- $dicms = @{
- msparams = $reParam.Matches($tokens[$i])
- ctr = 0
- }
- $tokens[$i] = Resolve-Token $reParam.Replace($dic[$func], $evaluator3) # 可能变成多token
- continue
- }
- $tokens[$i] = $reParam.Replace($tokens[$i], $evaluator1)
- }
- return Resolve-TokenTree $tokens
- }
- $evaluator1 = [System.Text.RegularExpressions.MatchEvaluator] {
- param($m)
- Resolve-Token $m.Value
- }
- $evaluator2 = [System.Text.RegularExpressions.MatchEvaluator] {
- param($m)
- if ($m.Value -match '^[A-Z]\w*$') {
- $dicms.tokens[$dicms.ctr++]
- } else { $m.Value }
- }
- $evaluator3 = [System.Text.RegularExpressions.MatchEvaluator] {
- param($m)
- if ($m.Value -match '^[A-Z]\w*$') {
- $dicms.msparams[$dicms.ctr++].Value
- } else { $m.Value }
- }
-
- foreach ($line in [IO.File]::ReadAllLines($dicFile, $ansi)) {
- if ($line -notmatch '^\s*#' -and $line -match '=') {
- $k, $v = $line -replace '\s+' -split '=', 2
- $dic[$k] = $v
- }
- }
- $lines = [IO.File]::ReadAllLines($srcFile, $ansi)
- for ($i = 0; $i -lt $lines.Count; $i++) {
- if ($lines[$i] -notmatch '^\s*#' -and $lines[$i] -match '=') {
- $lines[$i] = $lines[$i] -replace '\s+'
- $k, $v = $lines[$i] -split '=', 2
- $v2 = Resolve-Token $v
- $dic[$k] = $v2
- $lines[$i] = "${k}=${v2}"
- }
- }
-
- $lines
- Set-Content -Value $lines -LiteralPath $dstFile
复制代码
|