Internal Test: Implements C-style hexadecimal literals (e.g., 0xFF) by adding a new token rule and a token transformation.
ID: 1223
See: Dynamic Syntax
using uCalcSoftware;
var uc = new uCalc();
// 1. Define the lexical rule.
// The regex matches '0x' followed by hex digits.
// The TokenType::TokenTransform tells the parser to pre-process this token.
uc.ExpressionTokens.Add("0x[0-9a-fA-F]+", TokenType.TokenTransform);
// 2. Define the transformation rule.
// This captures the hex digits and replaces the whole token with a call to BaseConvert.
uc.TokenTransformer.FromTo("{'0x'}{val:'[0-9a-fA-F]+'}", "BaseConvert('{val}', 16)");
// 3. Now, the new literal format can be used in expressions.
Console.WriteLine(uc.Eval("0xFF + 0xA")); // 255 + 10
Console.WriteLine(uc.EvalStr("Hex(0x100)")); // Hex(256)
265
100 using uCalcSoftware; var uc = new uCalc(); // 1. Define the lexical rule. // The regex matches '0x' followed by hex digits. // The TokenType::TokenTransform tells the parser to pre-process this token. uc.ExpressionTokens.Add("0x[0-9a-fA-F]+", TokenType.TokenTransform); // 2. Define the transformation rule. // This captures the hex digits and replaces the whole token with a call to BaseConvert. uc.TokenTransformer.FromTo("{'0x'}{val:'[0-9a-fA-F]+'}", "BaseConvert('{val}', 16)"); // 3. Now, the new literal format can be used in expressions. Console.WriteLine(uc.Eval("0xFF + 0xA")); // 255 + 10 Console.WriteLine(uc.EvalStr("Hex(0x100)")); // Hex(256)
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// 1. Define the lexical rule.
// The regex matches '0x' followed by hex digits.
// The TokenType::TokenTransform tells the parser to pre-process this token.
uc.ExpressionTokens().Add("0x[0-9a-fA-F]+", TokenType::TokenTransform);
// 2. Define the transformation rule.
// This captures the hex digits and replaces the whole token with a call to BaseConvert.
uc.TokenTransformer().FromTo("{'0x'}{val:'[0-9a-fA-F]+'}", "BaseConvert('{val}', 16)");
// 3. Now, the new literal format can be used in expressions.
cout << uc.Eval("0xFF + 0xA") << endl; // 255 + 10
cout << uc.EvalStr("Hex(0x100)") << endl; // Hex(256)
}
265
100 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // 1. Define the lexical rule. // The regex matches '0x' followed by hex digits. // The TokenType::TokenTransform tells the parser to pre-process this token. uc.ExpressionTokens().Add("0x[0-9a-fA-F]+", TokenType::TokenTransform); // 2. Define the transformation rule. // This captures the hex digits and replaces the whole token with a call to BaseConvert. uc.TokenTransformer().FromTo("{'0x'}{val:'[0-9a-fA-F]+'}", "BaseConvert('{val}', 16)"); // 3. Now, the new literal format can be used in expressions. cout << uc.Eval("0xFF + 0xA") << endl; // 255 + 10 cout << uc.EvalStr("Hex(0x100)") << endl; // Hex(256) }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// 1. Define the lexical rule.
'// The regex matches '0x' followed by hex digits.
'// The TokenType::TokenTransform tells the parser to pre-process this token.
uc.ExpressionTokens.Add("0x[0-9a-fA-F]+", TokenType.TokenTransform)
'// 2. Define the transformation rule.
'// This captures the hex digits and replaces the whole token with a call to BaseConvert.
uc.TokenTransformer.FromTo("{'0x'}{val:'[0-9a-fA-F]+'}", "BaseConvert('{val}', 16)")
'// 3. Now, the new literal format can be used in expressions.
Console.WriteLine(uc.Eval("0xFF + 0xA")) '// 255 + 10
Console.WriteLine(uc.EvalStr("Hex(0x100)")) '// Hex(256)
End Sub
End Module
265
100 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// 1. Define the lexical rule. '// The regex matches '0x' followed by hex digits. '// The TokenType::TokenTransform tells the parser to pre-process this token. uc.ExpressionTokens.Add("0x[0-9a-fA-F]+", TokenType.TokenTransform) '// 2. Define the transformation rule. '// This captures the hex digits and replaces the whole token with a call to BaseConvert. uc.TokenTransformer.FromTo("{'0x'}{val:'[0-9a-fA-F]+'}", "BaseConvert('{val}', 16)") '// 3. Now, the new literal format can be used in expressions. Console.WriteLine(uc.Eval("0xFF + 0xA")) '// 255 + 10 Console.WriteLine(uc.EvalStr("Hex(0x100)")) '// Hex(256) End Sub End Module