2018-09-28 17:05:11 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2018 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace Asm65 {
|
|
|
|
|
public class Number {
|
|
|
|
|
/// <summary>
|
Allow explicit widths in project/platform symbols, part 1
The ability to give explicit widths to local variables worked out
pretty well, so we're going to try adding the same thing to project
and platform symbols.
The first step is to allow widths to be specified in platform files,
and set with the project symbol editor. The DefSymbol editor is
also used for local variables, so a bit of dancing is required.
For platform/project symbols the width is optional, and is totally
ignored for constants. (For variables, constants are used for the
StackRel args, so the width is meaningful and required.)
We also now show the symbol's type (address or constant) and width
in the listing. This gets really distracting when overused, so we
only show it when the width is explicitly set. The default width
is 1, which most things will be, so users can make an aesthetic
choice there. (The place where widths make very little sense is when
the symbol represents a code entry point, rather than a data item.)
The maximum width of a local variable is now 256, but it's not
allowed to overlap with other variables or run of the end of the
direct page. The maximum width of a platform/project symbol is
65536, with bank-wrap behavior TBD.
The local variable table editor now refers to stack-relative
constants as such, rather than simply "constant", to make it clear
that it's not just defining an 8-bit constant.
Widths have been added to a handful of Apple II platform defs.
2019-10-01 21:58:24 +00:00
|
|
|
|
/// Parses an integer in a variety of formats (hex, decimal, binary). We allow
|
|
|
|
|
/// hex to be identified with a leading '$' as well as "0x".
|
2019-12-30 01:59:35 +00:00
|
|
|
|
///
|
2018-09-28 17:05:11 +00:00
|
|
|
|
/// Trim whitespace before calling here.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">String to parse.</param>
|
|
|
|
|
/// <param name="val">Integer value of string.</param>
|
|
|
|
|
/// <param name="intBase">What base the string was in (2, 10, or 16).</param>
|
|
|
|
|
/// <returns>True if the parsing was successful.</returns>
|
|
|
|
|
public static bool TryParseInt(string str, out int val, out int intBase) {
|
|
|
|
|
if (string.IsNullOrEmpty(str)) {
|
|
|
|
|
val = intBase = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (str[0] == '$') {
|
|
|
|
|
intBase = 16;
|
|
|
|
|
str = str.Substring(1); // Convert functions don't like '$'
|
|
|
|
|
} else if (str.Length > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
|
|
|
|
|
intBase = 16;
|
|
|
|
|
} else if (str[0] == '%') {
|
|
|
|
|
intBase = 2;
|
|
|
|
|
str = str.Substring(1); // Convert functions don't like '%'
|
|
|
|
|
} else {
|
|
|
|
|
intBase = 10; // try it as decimal
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
val = Convert.ToInt32(str, intBase);
|
|
|
|
|
//Debug.WriteLine("GOT " + val + " - " + intBase);
|
|
|
|
|
} catch (Exception) {
|
|
|
|
|
//Debug.WriteLine("TryParseInt failed on '" + str + "': " + ex.Message);
|
|
|
|
|
val = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-12-30 01:59:35 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parses a long integer in a variety of formats (hex, decimal, binary). We allow
|
|
|
|
|
/// hex to be identified with a leading '$' as well as "0x".
|
|
|
|
|
///
|
|
|
|
|
/// Trim whitespace before calling here.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">String to parse.</param>
|
|
|
|
|
/// <param name="val">Integer value of string.</param>
|
|
|
|
|
/// <param name="intBase">What base the string was in (2, 10, or 16).</param>
|
|
|
|
|
/// <returns>True if the parsing was successful.</returns>
|
|
|
|
|
public static bool TryParseLong(string str, out long val, out int intBase) {
|
|
|
|
|
if (string.IsNullOrEmpty(str)) {
|
|
|
|
|
val = intBase = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (str[0] == '$') {
|
|
|
|
|
intBase = 16;
|
|
|
|
|
str = str.Substring(1); // Convert functions don't like '$'
|
|
|
|
|
} else if (str.Length > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
|
|
|
|
|
intBase = 16;
|
|
|
|
|
} else if (str[0] == '%') {
|
|
|
|
|
intBase = 2;
|
|
|
|
|
str = str.Substring(1); // Convert functions don't like '%'
|
|
|
|
|
} else {
|
|
|
|
|
intBase = 10; // try it as decimal
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
val = Convert.ToInt64(str, intBase);
|
|
|
|
|
//Debug.WriteLine("GOT " + val + " - " + intBase);
|
|
|
|
|
} catch (Exception) {
|
|
|
|
|
//Debug.WriteLine("TryParseInt failed on '" + str + "': " + ex.Message);
|
|
|
|
|
val = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-09-28 17:05:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|