Extends the parser to support C-style `0x` hex and `0b` binary notations using a token transformer.
ID: 342
See: Add(string, TokenType, string, int, RegExGrammar, int), ExpressionTokens = [Tokens], TokenTransformer = [Transformer]
using uCalcSoftware;
var uc = new uCalc();
// Define a token for C-like 0x hex notation
uc.ExpressionTokens.Add("0x[0-9A-Fa-f]+", TokenType.TokenTransform);
uc.TokenTransformer.FromTo("{'0x'}{Num:'[0-9A-Fa-f]+'}", "BaseConvert('{Num}', 16)");
Console.WriteLine($"0xFF is evaluated as: {uc.EvalStr("0xFF")}");
// Define a token for C++-style 0b binary notation
uc.ExpressionTokens.Add("0b[01]+", TokenType.TokenTransform);
// Using {@Eval} is more efficient as the conversion happens once during the token transform pass.
uc.TokenTransformer.FromTo("{'0b'}{Num:'[01]+'}", "{@Eval: BaseConvert(Num, 2)}");
Console.WriteLine($"0b1011 is evaluated as: {uc.EvalStr("0b1011")}");
// Note: uCalc has built-in support for hex/binary using # notation (e.g., #hFF, #b1011)
Console.WriteLine($"uCalc's built-in #hFF is: {uc.EvalStr("#hFF")}");
0xFF is evaluated as: 255
0b1011 is evaluated as: 11
uCalc's built-in #hFF is: 255 using uCalcSoftware; var uc = new uCalc(); // Define a token for C-like 0x hex notation uc.ExpressionTokens.Add("0x[0-9A-Fa-f]+", TokenType.TokenTransform); uc.TokenTransformer.FromTo("{'0x'}{Num:'[0-9A-Fa-f]+'}", "BaseConvert('{Num}', 16)"); Console.WriteLine($"0xFF is evaluated as: {uc.EvalStr("0xFF")}"); // Define a token for C++-style 0b binary notation uc.ExpressionTokens.Add("0b[01]+", TokenType.TokenTransform); // Using {@Eval} is more efficient as the conversion happens once during the token transform pass. uc.TokenTransformer.FromTo("{'0b'}{Num:'[01]+'}", "{@Eval: BaseConvert(Num, 2)}"); Console.WriteLine($"0b1011 is evaluated as: {uc.EvalStr("0b1011")}"); // Note: uCalc has built-in support for hex/binary using # notation (e.g., #hFF, #b1011) Console.WriteLine($"uCalc's built-in #hFF is: {uc.EvalStr("#hFF")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Define a token for C-like 0x hex notation
uc.ExpressionTokens().Add("0x[0-9A-Fa-f]+", TokenType::TokenTransform);
uc.TokenTransformer().FromTo("{'0x'}{Num:'[0-9A-Fa-f]+'}", "BaseConvert('{Num}', 16)");
cout << "0xFF is evaluated as: " << uc.EvalStr("0xFF") << endl;
// Define a token for C++-style 0b binary notation
uc.ExpressionTokens().Add("0b[01]+", TokenType::TokenTransform);
// Using {@Eval} is more efficient as the conversion happens once during the token transform pass.
uc.TokenTransformer().FromTo("{'0b'}{Num:'[01]+'}", "{@Eval: BaseConvert(Num, 2)}");
cout << "0b1011 is evaluated as: " << uc.EvalStr("0b1011") << endl;
// Note: uCalc has built-in support for hex/binary using # notation (e.g., #hFF, #b1011)
cout << "uCalc's built-in #hFF is: " << uc.EvalStr("#hFF") << endl;
}
0xFF is evaluated as: 255
0b1011 is evaluated as: 11
uCalc's built-in #hFF is: 255 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Define a token for C-like 0x hex notation uc.ExpressionTokens().Add("0x[0-9A-Fa-f]+", TokenType::TokenTransform); uc.TokenTransformer().FromTo("{'0x'}{Num:'[0-9A-Fa-f]+'}", "BaseConvert('{Num}', 16)"); cout << "0xFF is evaluated as: " << uc.EvalStr("0xFF") << endl; // Define a token for C++-style 0b binary notation uc.ExpressionTokens().Add("0b[01]+", TokenType::TokenTransform); // Using {@Eval} is more efficient as the conversion happens once during the token transform pass. uc.TokenTransformer().FromTo("{'0b'}{Num:'[01]+'}", "{@Eval: BaseConvert(Num, 2)}"); cout << "0b1011 is evaluated as: " << uc.EvalStr("0b1011") << endl; // Note: uCalc has built-in support for hex/binary using # notation (e.g., #hFF, #b1011) cout << "uCalc's built-in #hFF is: " << uc.EvalStr("#hFF") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Define a token for C-like 0x hex notation
uc.ExpressionTokens.Add("0x[0-9A-Fa-f]+", TokenType.TokenTransform)
uc.TokenTransformer.FromTo("{'0x'}{Num:'[0-9A-Fa-f]+'}", "BaseConvert('{Num}', 16)")
Console.WriteLine($"0xFF is evaluated as: {uc.EvalStr("0xFF")}")
'// Define a token for C++-style 0b binary notation
uc.ExpressionTokens.Add("0b[01]+", TokenType.TokenTransform)
'// Using {@Eval} is more efficient as the conversion happens once during the token transform pass.
uc.TokenTransformer.FromTo("{'0b'}{Num:'[01]+'}", "{@Eval: BaseConvert(Num, 2)}")
Console.WriteLine($"0b1011 is evaluated as: {uc.EvalStr("0b1011")}")
'// Note: uCalc has built-in support for hex/binary using # notation (e.g., #hFF, #b1011)
Console.WriteLine($"uCalc's built-in #hFF is: {uc.EvalStr("#hFF")}")
End Sub
End Module
0xFF is evaluated as: 255
0b1011 is evaluated as: 11
uCalc's built-in #hFF is: 255 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Define a token for C-like 0x hex notation uc.ExpressionTokens.Add("0x[0-9A-Fa-f]+", TokenType.TokenTransform) uc.TokenTransformer.FromTo("{'0x'}{Num:'[0-9A-Fa-f]+'}", "BaseConvert('{Num}', 16)") Console.WriteLine($"0xFF is evaluated as: {uc.EvalStr("0xFF")}") '// Define a token for C++-style 0b binary notation uc.ExpressionTokens.Add("0b[01]+", TokenType.TokenTransform) '// Using {@Eval} is more efficient as the conversion happens once during the token transform pass. uc.TokenTransformer.FromTo("{'0b'}{Num:'[01]+'}", "{@Eval: BaseConvert(Num, 2)}") Console.WriteLine($"0b1011 is evaluated as: {uc.EvalStr("0b1011")}") '// Note: uCalc has built-in support for hex/binary using # notation (e.g., #hFF, #b1011) Console.WriteLine($"uCalc's built-in #hFF is: {uc.EvalStr("#hFF")}") End Sub End Module