Name
Unisyn::Parse - Parse a Unisyn expression.
Synopsis
Parse a Unisyn expression.
Description
Parse a Unisyn expression.
Version "20210720".
The following sections describe the methods in each functional area of this module. For an alphabetic listing of all methods by name see Index.
Parse
Parse Unisyn expressions
parseUtf8(@parameters)
Parse a unisyn expression encoded as utf8
Parameter Description
1 @parameters Parameters
Example:
parseUtf8 Vq(address, $address), $size, $fail, $parse; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
Private Methods
getAlpha($register, $address, $index)
Load the position of a lexical item in its alphabet from the current character
Parameter Description
1 $register Register to load
2 $address Address of start of string
3 $index Index into string
getLexicalCode($register, $address, $index)
Load the lexical code of the current character in memory into the specified register.
Parameter Description
1 $register Register to load
2 $address Address of start of string
3 $index Index into string
putLexicalCode($register, $address, $index, $code)
Put the specified lexical code into the current character in memory.
Parameter Description
1 $register Register used to load code
2 $address Address of string
3 $index Index into string
4 $code Code to put
loadCurrentChar()
Load the details of the character currently being processed so that we have the index of the character in the upper half of the current character and the lexical type of the character in the lowest byte
checkStackHas($depth)
Check that we have at least the specified number of elements on the stack
Parameter Description
1 $depth Number of elements required on the stack
Example:
my @o = (Rb(reverse 0x10, 0, 0, 1), # Open bracket
Rb(reverse 0x11, 0, 0, 2), # Close bracket
Rb(reverse $Ascii, 0, 0, 27), # Ascii 'a'
Rb(reverse $variable, 0, 0, 27), # Variable 'a'
Rb(reverse $NewLineSemiColon, 0, 0, 0), # New line semicolon
Rb(reverse $semiColon, 0, 0, 0)); # Semi colon
for my $o(@o) # Try converting each input element
{Mov $start, $o;
Mov $index, 0;
loadCurrentChar;
PrintOutRegisterInHex $element;
}
ok Assemble(debug => 0, eq => <<END);
r13: 0000 0000 0000 0000
r13: 0000 0000 0000 0001
r13: 0000 0000 0000 0006
r13: 0000 0000 0000 0006
r13: 0000 0000 0000 0008
r13: 0000 0000 0000 0008
END
Push rbp;
Mov rbp, rsp;
Push rax;
Push rax;
checkStackHas 2; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
IfEq {PrintOutStringNL "ok"} sub {PrintOutStringNL "fail"};
checkStackHas 2; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
IfGe {PrintOutStringNL "ok"} sub {PrintOutStringNL "fail"};
checkStackHas 2; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
IfGt {PrintOutStringNL "fail"} sub {PrintOutStringNL "ok"};
Push rax;
checkStackHas 3; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
IfEq {PrintOutStringNL "ok"} sub {PrintOutStringNL "fail"};
checkStackHas 3; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
IfGe {PrintOutStringNL "ok"} sub {PrintOutStringNL "fail"};
checkStackHas 3; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
IfGt {PrintOutStringNL "fail"} sub {PrintOutStringNL "ok"};
ok Assemble(debug => 0, eq => <<END);
ok
ok
ok
ok
ok
ok
END
pushElement()
Push the current element on to the stack
pushEmpty()
Push the empty element on to the stack
Example:
Mov $index, 1;
pushEmpty; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
Mov rax, "[rsp]";
PrintOutRegisterInHex rax;
ok Assemble(debug => 0, eq => <<END);
Push Empty
rax: 0000 0001 0000 000A
END
lexicalNameFromLetter($l)
Lexical name for a lexical item described by its letter
Parameter Description
1 $l Letter of the lexical item
Example:
is_deeply lexicalNameFromLetter('a'), q(assign); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
is_deeply lexicalNumberFromLetter('a'), $assign;
lexicalNumberFromLetter($l)
Lexical number for a lexical item described by its letter
Parameter Description
1 $l Letter of the lexical item
Example:
is_deeply lexicalNameFromLetter('a'), q(assign);
is_deeply lexicalNumberFromLetter('a'), $assign; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
new($depth, $description)
Create a new term
Parameter Description
1 $depth Stack depth to be converted
2 $description Text reason why we are creating a new term
Example:
Mov $index, 1;
Mov rax,-1; Push rax;
Mov rax, 3; Push rax;
Mov rax, 2; Push rax;
Mov rax, 1; Push rax;
new 3, 'test'; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
Pop rax; PrintOutRegisterInHex rax;
Pop rax; PrintOutRegisterInHex rax;
ok Assemble(debug => 0, eq => <<END);
New: test
r8: 0000 0000 0000 0001
r8: 0000 0000 0000 0002
r8: 0000 0000 0000 0003
rax: 0000 0000 0000 0009
rax: FFFF FFFF FFFF FFFF
END
error($message)
Die
Parameter Description
1 $message Error message
Example:
error "aaa bbbb"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok Assemble(debug => 0, eq => <<END);
Error: aaa bbbb
Element: r13: 0000 0000 0000 0000
Index : r12: 0000 0000 0000 0000
END
testSet($set, $register)
Test a set of items, setting the Zero Flag is one matches else clear the Zero flag
Parameter Description
1 $set Set of lexical letters
2 $register Register to test
Example:
Mov r15, -1;
Mov r15b, $term;
testSet("ast", r15); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
PrintOutZF;
testSet("as", r15); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
PrintOutZF;
ok Assemble(debug => 0, eq => <<END);
ZF=1
ZF=0
END
checkSet($set)
Check that one of a set of items is on the top of the stack or complain if it is not
Parameter Description
1 $set Set of lexical letters
Example:
Mov r15, -1;
Mov r15b, $term;
Push r15;
checkSet("ast"); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
PrintOutZF;
checkSet("as"); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
PrintOutZF;
ok Assemble(debug => 0, eq => <<END);
ZF=1
Error: Expected one of: 'as' on the stack
Element: r13: 0000 0000 0000 0000
Index : r12: 0000 0000 0000 0000
END
reduce($priority)
Convert the longest possible expression on top of the stack into a term at the specified priority
Parameter Description
1 $priority Priority of the operators to reduce
Example:
Mov r15, -1; Push r15;
Mov r15, $term; Push r15;
Mov r15, $assign; Push r15;
Mov r15, $term; Push r15;
reduce 1; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
Pop r15; PrintOutRegisterInHex r15;
Pop r14; PrintOutRegisterInHex r14;
ok Assemble(debug => 0, eq => <<END);
Reduce 3:
r8: 0000 0000 0000 0009
r9: 0000 0000 0000 0005
r10: 0000 0000 0000 0009
New: Term infix term
r8: 0000 0000 0000 0009
r8: 0000 0000 0000 0009
r8: 0000 0000 0000 0005
r15: 0000 0000 0000 0009
r14: FFFF FFFF FFFF FFFF
END
reduceMultiple($priority)
Reduce existing operators on the stack
Parameter Description
1 $priority Priority of the operators to reduce
Example:
Mov r15, -1; Push r15;
Mov r15, $OpenBracket; Push r15;
reduceMultiple 1; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
Pop r15; PrintOutRegisterInHex r15;
Pop r14; PrintOutRegisterInHex r14;
ok Assemble(debug => 0, eq => <<END);
Reduce 2:
r8: 0000 0000 0000 0010
r9: 0000 0000 0000 0000
r15: 0000 0000 0000 0000
r14: FFFF FFFF FFFF FFFF
END
accept_a()
Assign
accept_b()
Open
accept_B()
Closing parenthesis
accept_d()
Infix but not assign or semi-colon
accept_p()
Prefix
accept_q()
Post fix
accept_s()
Semi colon
accept_v()
Variable
parseExpressionCode()
Parse the string of classified lexical items addressed by register $start of length $length. The resulting parse tree (if any) is returned in r15.
parseExpression(@parameters)
Create a parser for an expression described by variables
Parameter Description
1 @parameters Parameters describing expression
MatchBrackets(@parameters)
Replace the low three bytes of a utf32 bracket character with 24 bits of offset to the matching opening or closing bracket. Opening brackets have even codes from 0x10 to 0x4e while the corresponding closing bracket has a code one higher.
Parameter Description
1 @parameters Parameters
Example:
my $l = $Lex->{sampleLexicals}{brackets};
Mov $start, Rd(@$l);
Mov $size, scalar(@$l);
parseExpressionCode;
PrintOutStringNL "Result:";
PrintOutRegisterInHex r15;
ok Assemble(debug => 0, eq => <<END);
Push Element:
r13: 0000 0000 0000 0006
New: accept initial variable
r8: 0000 0000 0000 0006
r13: 0000 0001 0000 0005
accept a
Push Element:
r13: 0000 0001 0000 0005
r13: 0000 0002 0000 0000
accept b
Push Element:
r13: 0000 0002 0000 0000
r13: 0000 0003 0000 0000
accept b
Push Element:
r13: 0000 0003 0000 0000
r13: 0000 0004 0000 0000
accept b
Push Element:
r13: 0000 0004 0000 0000
r13: 0000 0005 0000 0006
accept v
Push Element:
r13: 0000 0005 0000 0006
New: Variable
r8: 0000 0005 0000 0006
r13: 0000 0006 0000 0001
accept B
Reduce 3:
r8: 0000 0003 0000 0000
r9: 0000 0004 0000 0000
r10: 0000 0000 0000 0009
Reduce 2:
r8: 0000 0000 0000 0030
r9: 0000 0004 0000 0000
Push Element:
r13: 0000 0006 0000 0001
Reduce 3:
r8: 0000 0004 0000 0000
r9: 0000 0000 0000 0009
r10: 0000 0006 0000 0001
Reduce by ( term )
Reduce 3:
r8: 0000 0002 0000 0000
r9: 0000 0003 0000 0000
r10: 0000 0000 0000 0009
Reduce 2:
r8: 0000 0000 0000 0028
r9: 0000 0003 0000 0000
r13: 0000 0007 0000 0001
accept B
Reduce 3:
r8: 0000 0002 0000 0000
r9: 0000 0003 0000 0000
r10: 0000 0000 0000 0009
Reduce 2:
r8: 0000 0000 0000 0028
r9: 0000 0003 0000 0000
Push Element:
r13: 0000 0007 0000 0001
Reduce 3:
r8: 0000 0003 0000 0000
r9: 0000 0000 0000 0009
r10: 0000 0007 0000 0001
Reduce by ( term )
Reduce 3:
r8: 0000 0001 0000 0005
r9: 0000 0002 0000 0000
r10: 0000 0000 0000 0009
Reduce 2:
r8: 0000 0000 0000 0020
r9: 0000 0002 0000 0000
r13: 0000 0008 0000 0003
accept d
Push Element:
r13: 0000 0008 0000 0003
r13: 0000 0009 0000 0000
accept b
Push Element:
r13: 0000 0009 0000 0000
r13: 0000 000A 0000 0006
accept v
Push Element:
r13: 0000 000A 0000 0006
New: Variable
r8: 0000 000A 0000 0006
r13: 0000 000B 0000 0001
accept B
Reduce 3:
r8: 0000 0008 0000 0003
r9: 0000 0009 0000 0000
r10: 0000 0000 0000 0009
Reduce 2:
r8: 0000 0000 0000 0038
r9: 0000 0009 0000 0000
Push Element:
r13: 0000 000B 0000 0001
Reduce 3:
r8: 0000 0009 0000 0000
r9: 0000 0000 0000 0009
r10: 0000 000B 0000 0001
Reduce by ( term )
Reduce 3:
r8: 0000 0000 0000 0009
r9: 0000 0008 0000 0003
r10: 0000 0000 0000 0009
New: Term infix term
r8: 0000 0000 0000 0009
r8: 0000 0000 0000 0009
r8: 0000 0008 0000 0003
Reduce 3:
r8: 0000 0001 0000 0005
r9: 0000 0002 0000 0000
r10: 0000 0000 0000 0009
Reduce 2:
r8: 0000 0000 0000 0020
r9: 0000 0002 0000 0000
r13: 0000 000C 0000 0001
accept B
Reduce 3:
r8: 0000 0001 0000 0005
r9: 0000 0002 0000 0000
r10: 0000 0000 0000 0009
Reduce 2:
r8: 0000 0000 0000 0020
r9: 0000 0002 0000 0000
Push Element:
r13: 0000 000C 0000 0001
Reduce 3:
r8: 0000 0002 0000 0000
r9: 0000 0000 0000 0009
r10: 0000 000C 0000 0001
Reduce by ( term )
Reduce 3:
r8: 0000 0000 0000 0009
r9: 0000 0001 0000 0005
r10: 0000 0000 0000 0009
New: Term infix term
r8: 0000 0000 0000 0009
r8: 0000 0000 0000 0009
r8: 0000 0001 0000 0005
r13: 0000 000D 0000 0008
accept s
Push Element:
r13: 0000 000D 0000 0008
Result:
r15: 0000 0000 0000 0009
END
ClassifyNewLines(@parameters)
Scan input string looking for opportunities to convert new lines into semi colons
Parameter Description
1 @parameters Parameters
ClassifyWhiteSpace(@parameters)
Classify white space per: "lib/Unisyn/whiteSpace/whiteSpaceClassification.pl"
Parameter Description
1 @parameters Parameters
T($key, $expected)
Test a parse
Parameter Description
1 $key Key of text to be parsed
2 $expected Expected result
Index
1 accept_a - Assign
2 accept_B - Closing parenthesis
3 accept_b - Open
4 accept_d - Infix but not assign or semi-colon
5 accept_p - Prefix
6 accept_q - Post fix
7 accept_s - Semi colon
8 accept_v - Variable
9 checkSet - Check that one of a set of items is on the top of the stack or complain if it is not
10 checkStackHas - Check that we have at least the specified number of elements on the stack
11 ClassifyNewLines - Scan input string looking for opportunities to convert new lines into semi colons
12 ClassifyWhiteSpace - Classify white space per: "lib/Unisyn/whiteSpace/whiteSpaceClassification.
13 error - Die
14 getAlpha - Load the position of a lexical item in its alphabet from the current character
15 getLexicalCode - Load the lexical code of the current character in memory into the specified register.
16 lexicalNameFromLetter - Lexical name for a lexical item described by its letter
17 lexicalNumberFromLetter - Lexical number for a lexical item described by its letter
18 loadCurrentChar - Load the details of the character currently being processed so that we have the index of the character in the upper half of the current character and the lexical type of the character in the lowest byte
19 MatchBrackets - Replace the low three bytes of a utf32 bracket character with 24 bits of offset to the matching opening or closing bracket.
20 new - Create a new term
21 parseExpression - Create a parser for an expression described by variables
22 parseExpressionCode - Parse the string of classified lexical items addressed by register $start of length $length.
23 parseUtf8 - Parse a unisyn expression encoded as utf8
24 pushElement - Push the current element on to the stack
25 pushEmpty - Push the empty element on to the stack
26 putLexicalCode - Put the specified lexical code into the current character in memory.
27 reduce - Convert the longest possible expression on top of the stack into a term at the specified priority
28 reduceMultiple - Reduce existing operators on the stack
29 T - Test a parse
30 testSet - Test a set of items, setting the Zero Flag is one matches else clear the Zero flag
Installation
This module is written in 100% Pure Perl and, thus, it is easy to read, comprehend, use, modify and install via cpan:
sudo cpan install Unisyn::Parse
Author
Copyright
Copyright (c) 2016-2021 Philip R Brenan.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.