1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import { LanguageService } from "../services/languageService";
- import { Types } from "./types";
- import Decimal from "decimal.js";
- import { Config } from "../util/config";
- export function toInt (str) {
- return new Decimal(str);
- }
- export function toString (str) {
- let value = str.replace(/^"/, "");
- value = value.replace(/"$/, "");
- value = value.replace(/\\b/g, "\b");
- value = value.replace(/\\t/g, "\t");
- value = value.replace(/\\n/g, "\n");
- value = value.replace(/\\r/g, "\r");
- value = value.replace(/\\"/g, '"');
- value = value.replace(/\\'/g, "'");
- value = value.replace(/\\\\/g, "\\");
- return value;
- }
- export function toChar (str) {
- //console.debug(str, str.length);
- let value = str.replace(/^'/, "");
- value = value.replace(/'$/, "");
- value = value.replace(/\\b/g, "\b");
- value = value.replace(/\\t/g, "\t");
- value = value.replace(/\\n/g, "\n");
- value = value.replace(/\\r/g, "\r");
- value = value.replace(/\\"/g, '"');
- value = value.replace(/\\'/g, "'");
- value = value[0];
- return value;
- }
- export function toReal (value) {
- return new Decimal(value);
- }
- export function toBool (str) {
- const val = str;
- const lexer = LanguageService.getCurrentLexer();
- if (lexer.getReservedKeys()[lexer.getRules().RK_TRUE] === val) {
- return true;
- } else if (lexer.getReservedKeys()[lexer.getRules().RK_FALSE] === val) {
- return false;
- } else {
- // TODO: better error message
- throw new Error(str + "not a valid boolean");
- }
- }
- export function convertBoolToString (bool) {
- const lexer = LanguageService.getCurrentLexer();
- let result = null;
- if (bool) {
- result = lexer.getReservedKeys()[lexer.getRules().RK_TRUE];
- } else {
- result = lexer.getReservedKeys()[lexer.getRules().RK_FALSE];
- }
- //console.debug(result)
- return result.replace(/'/g, "");
- }
- export function convertToString (value, type) {
- switch (type.ord) {
- case Types.INTEGER.ord:
- return value.toString();
- case Types.REAL.ord: {
- if (value.dp() <= 0) {
- return value.toFixed(1);
- } else {
- return value.toFixed(Config.decimalPlaces);
- }
- }
- case Types.BOOLEAN.ord:
- return convertBoolToString(value);
- default:
- return value;
- }
- }
|