ImplicitDim.bas
Global MyGlobal_A As Long, MyGlobal_B As Dword
Function MyFunction(x, y, z) As Long
Dim MyString As String, abcd As Long
Print x, y, z
Print a, b, c
Print MyGlobal_A, MyGlobal_B
Print MyString, abcd
Function = Len(MyString)
End Function
Sub MySub(ByRef Arg_A As Extended, ByVal Arg_B As Long, ByVal Arg_C!)
Dim MyLocal_A As String, MyLocal_B As Long
Print Implicit_X, Implicit_Y, Implicit_Z
Print Arg_A, Arg_B, Arg_C!
Print MyGlobal_A, MyGlobal_B
Print MyLocal_A, MyLocal_B
End Function
ImplicitDim.uc
# ImplicitDim.uc - uCalc Transformation file
# This file was saved with uCalc Transform 2.0 on 10/15/2013 7:08:31 PM
# Comment: Declares variables (with Dim) that were not explicitely declared before
ExternalKeywords: Exclude, Comment, Selected, ParentChild, FindMode, OutputFile, BatchAction, SEND
ExternalKeywords: Highlight, ForeColor, BackColor, FontName, FontSize, FontStyle
ExternalKeywords: FilterEndText, FilterSeparator, FilterSort, FilterSortFunc, FilterStartText, FilterUnique
FindMode: Replace
# Definitions
# Search Criteria
Criteria: 0
Enabled: True
Exclude: False
Comment: Declares variables (with Dim) that were not explicitely declared before
Selected: False
Highlight: False
ForeColor: ControlText
BackColor: Aqua
FontName:
FontSize:
FontStyle:
CaseSensitive: False
QuoteSensitive: True
CodeBlockSensitive: True
FilterEndText:
FilterSeparator: {#10}
FilterSort: False
FilterSortFunc:
FilterStartText:
FilterUnique: False
Min: 0
Max: -1
MinSoft: 0
MaxSoft: -1
BatchAction: Transform
OutputFile:
SEND:
StartAfter: 0
StopAfter: -1
SkipOver: False
ParentChild: 0
Pass: 0
PassOnce: True
Precedence: 0
RightToLeft: False
Criteria: 1
Find:
Replace: {@Define:: Token: \x27.* ~~ Properties: ucWhitespace}
{@Eval: Dim Globals As Table, CurrentRoutine As String}
Criteria: 2
Comment: Teporarily adds Dim keyword in front of args for further parsing
Pass: 1
Criteria: 3
Selected: True
Find: {nl}{routine: Sub | Function } {etc}({args})
Replace: {nl}{routine} {etc}(Dim {args})
Criteria: 4
SkipOver: True
Find: {nl}Function =
Replace: [Skip over]
Criteria: 5
Comment: Inserts explicitly Dimmed variable names in global or local tables
Pass: 2
Criteria: 6
Find: Global {variable:1}
Replace: {Self}{@Eval: Insert(Globals, "{variable}")}
Criteria: 7
BackColor: Silver
Find: { {nl}{ Macro | Type | Union | % | $ | Declare {func:1} } | . | @ } {name:1}
Replace: {Self}{@Eval: Insert(Globals, "{name}")}
Criteria: 8
BackColor: Lime
Find: {nl}{ Sub | Function }{" +"}{RoutineName:"[a-z0-9_]+"}
Replace: {Self}{@Define:
Var: {RoutineName}ExplicitDim As Table
Var: {RoutineName}ImplicitDim As Table
}{@Eval: SetVar(CurrentRoutine, "{RoutineName}"); Insert(Globals,"{RoutineName}")}
Criteria: 9
BackColor: Pink
Find: {declare: Dim [[Optional] { ByVal | ByRef }] | Local | Static } {variable:1}
Replace: {Self}{@Eval: Insert(~Eval(CurrentRoutine)ExplicitDim, "{variable}")}
Criteria: 10
Comment: Temporarily inserts declaration for each individual variable a lines with a list of multiple vars, for easier parsing
BackColor: SlateBlue
PassOnce: False
Find: {declare: Global | Dim | Local | Static } {variable},
Replace: {declare} {variable} :: {declare}
Criteria: 11
Comment: Places non-Dimmed variable names in separate local tables
Pass: 3
Criteria: 12
Find: {variable:"[a-z][a-z0-9_]*"}
Replace: {Self}{@Eval:
IIf(Handle(Globals, "{variable}")==0 And Handle(~Eval(CurrentRoutine)ExplicitDim, "{variable}")==0,
Insert(~Eval(CurrentRoutine)ImplicitDim, "{variable}"); "")
}
Criteria: 13
SkipOver: True
Find: {@Eval: "{'"+Retain(FileText("PBKeywords.txt"), "{keyword:'[a-z0-9_]+'}", Delim("\b|"))+"\b'}"}
Replace: [Skip over]
Criteria: 14
BackColor: Brown
Find: {"\n"}{ Sub | Function }{" +"}{RoutineName:"[a-z0-9_]+"}
Replace: {Self}{@Eval: SetVar(CurrentRoutine, "{RoutineName}")}
Criteria: 15
Comment: Inserts local Dim statements for variables that were not dimmed
Pass: 4
Criteria: 16
BackColor: Purple
Find: {nl}{ Sub | Function }{" +"}{RoutineName:"[a-z0-9_]+"} {etc}
Replace: {Self}
Dim {@Eval: Range(1, Count({RoutineName}ImplicitDim), "ReadKey({RoutineName}ImplicitDim, x)", Delim(", "))} ' Implicit
Criteria: 17
Comment: Clean up (temp declaration statements removed)
Pass: 5
Criteria: 18
BackColor: Violet
Find: :: {declare:1}
Replace: ,
Criteria: 19
BackColor: CornflowerBlue
Find: (Dim {args})
Replace: ({@Eval: Replace("{args}", ":: Dim", ",")})
Criteria: 20
Find: Dim {nl}
Replace: {Nothing}
# End Search
PrettyBas.bas
' The source code below is intentionally formatted incorrectly.
' PrettyBas.uc will correct spacing & indentation and will add color.
' There are two ways to test PrettyBas.uc with this file:
' A. Right-click this file from within uCalc and select:
' Open With Indentation/Color
' (In this case \PrettyBas.uc from the .\Transforms\ directory is used
and is part of the setup defined in .\FileTypes\Bas)
' B. To fiddle with the transform interactively click the folder button
and choose PrettyBas.uc as a transform:
' (In this case \PrettyBas.uc from the .\Examples\PowerBASIC\ directory is used)
#Compile Exe
#Dim All
#Include "Win32API.inc" ' Includes the API for Windows
Type MyType ' These will be indented properly
x As Long
abc As Long
xyz As Long
Something As Long
Other As Long
End Type
Function TEST(ByVal num As Long, MyStr As String , xyz As Long) As Dword ' Just a test
Dim x As Long ,y As Long, value&, t!
t! = Timer
! FNCLEX ; some random ASM
! MOV AX , "xy"
! MOV EAX, value&
For X = 1 To 10 ' The indentation will take care of this nested code
For Y = 2 To 20
If x = 1 Then y=2
If x= 5 Then
If Y = X Then Print "Testing 'testing' 123" ' Just testing
value& = value& + 1 *x * xyz -5^ 2
If MyStr = "Something ""or"" other" Then value& =Len(MyStr)*2
End If
Next
Print Timer-t!
Next
Function = 123+Len(MyStr)+num /2
End Function
Function PBMain () As Long
Dim x As Long ,y As Long, z As String, FileNumber As Long
FileNumber = FreeFile
Open "c:\test\kjv10.txt" For Binary As #FileNumber
If Lof(#FileNumber) > 1000 Then ' big enough
While Rnd() < .25
For x= 1 To 10
Print Test(x +123 ,"abc xyz",123)
For y = 1 To 10 : Print x, y: Next
While Rnd()*10 < 5 :Print y : Wend
Next
Wend
End If
Data "One","Two", "Three" , "Four", 5 ,6 , 7, 8
End Function
PrettyBas.uc
# PrettyBas.uc - uCalc Transformation file
# This file was saved with uCalc Transform 1.9 on 10/13/2013 2:21:39 PM
# Comment: Adds color and indentation to BASIC source code
ExternalKeywords: Exclude, Comment, Selected, ParentChild, FindMode, OutputFile, BatchAction, SEND
ExternalKeywords: Highlight, ForeColor, BackColor, FontName, FontSize, FontStyle
ExternalKeywords: FilterEndText, FilterSeparator, FilterSort, FilterSortFunc, FilterStartText, FilterUnique
FindMode: Replace
# Definitions
# Search Criteria
Criteria: 0
Enabled: True
Exclude: False
Comment: Adds color and indentation to BASIC source code
Selected: False
Highlight: False
ForeColor: ControlText
BackColor: Transparent
FontName:
FontSize:
FontStyle:
CaseSensitive: False
QuoteSensitive: True
CodeBlockSensitive: False
FilterEndText:
FilterSeparator: {#10}
FilterSort: False
FilterSortFunc:
FilterStartText:
FilterUnique: False
Min: 0
Max: -1
MinSoft: 0
MaxSoft: -1
BatchAction: Transform
OutputFile:
SEND:
StartAfter: 0
StopAfter: -1
SkipOver: False
ParentChild: 0
Pass: 0
PassOnce: False
Precedence: 0
RightToLeft: False
Criteria: 1
BackColor: CornflowerBlue
Find: {@Note: == Explanation ==
This section here, enlcosed within {@Note: ...} represents a comment.
Preliminary items are defined on the right in preparation for
transformation operations below.
{@Define: ...} with one colon defines things in the "Evaluation" space,
which is kept separate from the space of text being parsed.
{@Define:: ...} (2 colons) defines things in the space of text being
parsed. Alphanumeric tokens are redefined to support PB prefixes/suffixes.
}
Replace: {@Define:
Var: Indentation = 0
Const: IndentSize = 3
Inline: {indent+} := Indentation += IndentSize
Inline: {indent-} := Indentation -= IndentSize
Inline: {indent} := " " * Indentation
}
{@Define:: Include: PBTokens.uc}
Criteria: 2
Comment: Adds indentation
Pass: 1
Criteria: 3
Selected: True
Find: {@Start}
Replace: {@Eval: Indentation = 0}
Criteria: 4
Comment: {nl} represents New Line (defined as such in Patterns.uc), and {etc} is just a pattern variable
PassOnce: True
Find: {nl}{etc}
Replace: {nl}{indent}{etc}
Criteria: 5
Comment: Indents tag then increments indentation
PassOnce: True
Find: {nl}{StartWord: Type | Union | For | If | Do | While | Macro | Sub | Function | Select}
Replace: {nl}{indent}{StartWord}{indent+}
Criteria: 6
PassOnce: True
Find: {nl}{MiddleWord: Else | ElseIf | Case}
Replace: {nl}{indent-}{indent}{MiddleWord}{indent+}
Criteria: 7
Comment: Indents end-tag then decrements indentation
PassOnce: True
Find: {nl}{EndWord: End | Next | Loop | Wend }
Replace: {nl}{indent-}{indent}{EndWord}
Criteria: 8
PassOnce: True
Find: {nl}{SameLine: For | Do | While } {etc} :
Replace: {nl}{indent}{SameLine} {etc} :
Criteria: 9
Comment: Doesn't increase indentation for single-line If statement
PassOnce: True
Find: {nl}If {cond} { Then | Goto} {Space:"[ ]+"} {SameLine:"[^'\r\n]"}
Replace: {nl}{indent}If {cond} Then {SameLine}
Criteria: 10
PassOnce: True
Find: {nl}{Other: Function = | Macro {def} = }
Replace: {nl}{indent}{Other}
Criteria: 11
Comment: Adds color
Pass: 2
Criteria: 12
Comment: Adds color to value within quotes
Highlight: True
ForeColor: Brown
PassOnce: True
Find: {QuotedText:"[\q][^\q]*[\q]"}
Replace: {QuotedText}
Criteria: 13
Comment: {@Eval: ...} is replaced with ! | #Bloat | #Compile | #Compiler | #Debug | ...
Highlight: True
ForeColor: Blue
PassOnce: True
Find: {Keyword: {@Eval: Retain(Remove(FileText("PBKeywords.txt"), "{'\x27'}"), "{'.*'}", Delim(" | ")) } }
Replace: {Keyword}
Criteria: 14
Highlight: True
ForeColor: Green
PassOnce: True
Find: {Comment:"'.*"}
Replace: {Comment}
Criteria: 15
Highlight: True
ForeColor: Red
PassOnce: True
Find: ! {ASM}
Replace: ! {ASM}
Criteria: 16
Comment: Normalizes spacing between arithmetic symbols like +, -, *, etc.
ForeColor: Black
PassOnce: True
Find: [{"[ ]+"}] {Symbol:"[\=\:\+\-\*\^\\\/]"} [{"[ ]+"}]
Replace: {sp}{Symbol}{sp}
Criteria: 17
Comment: Normalizes spacing around commas
ForeColor: Black
PassOnce: True
Find: {Comma:" *, *"}
Replace: ,{sp}
# End Search