Skip to content

Commit 5413bbd

Browse files
author
aafent
committed
New Library for Date and Time functions
1 parent 0a61e65 commit 5413bbd

File tree

7 files changed

+352
-24
lines changed

7 files changed

+352
-24
lines changed

FAST.FBasic.InteractiveConsole/FBasicIC.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ private void runFBasicProgram()
148148
interp.SetVar("table.column", new Value("myColumn1"));
149149
interp.AddLibrary(new FBasicStringFunctions());
150150
interp.AddLibrary(new FBasicMathFunctions());
151+
interp.AddLibrary(new FBasicDateFunctions());
151152
});
152153
if (result.hasError)
153154
{

FAST.FBasicInterpreter/Core/Errors.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ public static string E112_UndeclaredVariable(string varName)
2222
public static string E125_WrongNumberOfArguments(int argNo, string syntax = "")
2323
{
2424
// Wrong number of arguments. Expected {argNo}. {syntax} [E125]
25-
string msg = $"Wrong number of arguments. Expected {argNo}.";
25+
string msg;
26+
if (argNo==0)
27+
{
28+
msg = $"Wrong number of arguments. No arguments are expected.";
29+
} else
30+
{
31+
msg = $"Wrong number of arguments. Expected {argNo}.";
32+
}
33+
34+
2635
if (!string.IsNullOrEmpty(syntax)) msg += syntax;
2736
msg += " [E125]";
2837
return msg;
2938
}
39+
40+
3041
public static string E126_WrongArgumentType(int argNo=0, string syntax = "")
3142
{
3243
// Wrong argument(s) type. Check argument: {argNo}. {syntax} [E126]
@@ -45,6 +56,7 @@ public static string E127_WrongArgumentReferredType(string expectedType, string
4556
return msg;
4657
}
4758

59+
4860
public static string X007_OnlyUnaryOperationsOnNumbers()
4961
{
5062
return "Can only do unary operations on numbers. [X007]";

FAST.FBasicInterpreter/Core/Value.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace FAST.FBasicInterpreter
1+
using System.Reflection.Metadata;
2+
3+
namespace FAST.FBasicInterpreter
24
{
35
public enum ValueType
46
{
@@ -8,8 +10,32 @@ public enum ValueType
810

911
public struct Value
1012
{
13+
/// <summary>
14+
/// The numeric value of True
15+
/// </summary>
16+
public const int TrueValue = 1;
17+
18+
/// <summary>
19+
/// The numeric value of False
20+
/// </summary>
21+
public const int FalseValue=-1;
22+
23+
/// <summary>
24+
/// The value used as Zero
25+
/// </summary>
1126
public static readonly Value Zero = new Value(0);
1227

28+
/// <summary>
29+
/// Value for the boolean true
30+
/// </summary>
31+
public static readonly Value True = new Value(TrueValue);
32+
33+
/// <summary>
34+
/// Value for the boolean false
35+
/// </summary>
36+
public static readonly Value False = new Value(FalseValue);
37+
38+
1339
/// <summary>
1440
/// Used to return an Error.
1541
/// Any Error casted to 0 value
@@ -26,6 +52,17 @@ public Value(double real) : this()
2652
this.Type = ValueType.Real;
2753
this.Real = real;
2854
}
55+
public Value(int value)
56+
{
57+
this.Type = ValueType.Real;
58+
this.Real = value;
59+
}
60+
public Value(bool value)
61+
{
62+
this.Type = ValueType.Real;
63+
this.Real = value?TrueValue:FalseValue;
64+
}
65+
2966

3067
public Value(string str): this()
3168
{
@@ -138,10 +175,33 @@ public override string ToString()
138175
return this.String;
139176
}
140177

178+
/// <summary>
179+
/// Convert an arithmetic value to integer
180+
/// </summary>
181+
/// <returns>Int</returns>
141182
public int ToInt()
142183
{
143184
if (this.Type == ValueType.Real) return (int)this.Real;
144185
throw new Exception(Errors.X011_CannotConvert("String","Integer"));
145186
}
187+
188+
/// <summary>
189+
/// Convert the current arithmetic value to boolean
190+
/// </summary>
191+
/// <returns>bool</returns>
192+
public bool ToBool() => this.Real==TrueValue?true:false;
193+
194+
/// <summary>
195+
/// Check if the value is false
196+
/// </summary>
197+
/// <returns>bool</returns>
198+
public bool IsFalse() => ToBool()==false;
199+
200+
/// <summary>
201+
/// Check if the value is true
202+
/// </summary>
203+
/// <returns></returns>
204+
public bool IsTrue() => !IsFalse();
205+
146206
}
147207
}

FAST.FBasicInterpreter/Documents/FBasicManual.md

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@ In this manual you can find the following chapters
1111
3. Statements
1212
4. Debugging
1313
5. Comments
14-
6. Functions
15-
7. Variables
16-
8. Collections
17-
9. Operators
18-
10. Libraries and Add-Ons
19-
1\. String functions
20-
21-
2\. Mathimatical functions
22-
23-
3\. SQL Data provider 
24-
25-
11. Handlers
14+
6. Datatypes
15+
7. Functions
16+
8. Variables
17+
9. Collections
18+
10. Operators
19+
11. Libraries and Add-Ons
20+
1\. String functions
21+
2\. Mathematical functions
22+
3\. Date and Time functions 
23+
4\. SQL Data provider
24+
12. Handlers
2625

2726
### Flow Control
2827

@@ -167,6 +166,19 @@ _example:_
167166
> let a=10 ' set the value 10 to variable a
168167
> ```
169168
169+
### Datatypes
170+
171+
FBASIC, as programming language, typically offers two primary data types: **String** and **Real**.
172+
173+
* A **String** is a sequence of characters, such as letters, numbers, or symbols, enclosed in double quotes. It's used for handling text.
174+
* A **Real** (also known as a floating-point number) represents a number with a fractional component. It's used for all numerical calculations, from simple arithmetic to complex equations.
175+
176+
In FBASIC interpreter, other data types like **Integer**, **Boolean**, **Date**, and **Time** are not natively supported. The FBASIC simulate these by using the existing **Real** and **String** types and applying specific conventions or libraries to manage them. For instance, integers are stored as **Real** numbers, and logical values (true/false) can be represented by numeric value. This simplifies the language while still allowing for a wide range of programming tasks.
177+
178+
Especially for **Date** and **Time** datatypes check this manual at libraries chapter (Date & Time libraries). 
179+
180+
About the Boolean values there a need to explain further the concept. In most of the BASIC implementations, **0 is false** and **\-1 is true**.   Why this?  In most modern programming languages, the integer 0 represents **false**, and any non-zero integer represents **true**. However, in early versions of BASIC, a different convention was adopted. This was due to the way logical operations were implemented at the machine-code level. The bitwise operations for _AND_, _OR_, and _NOT_ were often optimized to work with all the bits of a value. A bitwise _NOT_ of 00000000 (which is 0 in a byte) results in 11111111, which is -1 in two's complement representation. This made it convenient to use -1 for true, as it was the direct result of a _NOT_ operation on the false value (0).
181+
170182
### Functions
171183
172184
#### Built-ins functions:
@@ -221,7 +233,28 @@ FBASIC,  includes a variety of built-in mathematical functions to perform commo
221233
222234
as the FBASIC does not support constants, a constant is a variable with a value. Its up to the programmer to avoid to modify that value. 
223235
224-
* **PI:** The PI value, approximately equal to 3.14159. 
236+
* **PI:** The PI value, approximately equal to 3.14159.
237+
238+
#### Date & Time Functions
239+
240+
Date and Time functions in the BASIC programming language are used to handle and manipulate dates and times. These functions allow programs to retrieve the current date and time, perform calculations with dates, and format dates for display or any other usage. The native date format is the DD-MM-YYYY. As the FBASIC does not support date as datatype, the functions will consider any valid value having this format as a date. 
241+
242+
Date and Time functions listed here are available if library _FBasicDateFunctions_ is enabled.
243+
244+
* **date():** This function returns a string representing the current system date. The format typically includes the day, month, and year.
245+
* **isdate(d)**: Check if the input string is a valid date.
246+
* **day():** Extract the day as an numeric value (integer) from a date variable. For example, day("26-10-2025") would return 26.
247+
* **month():** Extract the month as an numeric value (integer) from a date variable. For example, month("26-10-2025") would return 10.
248+
* **year():** Extract the year as an numeric value (integer) from a date variable. For example, year("26-10-2025") would return 2025.
249+
* **jtod():** Converts the japan style of date (YYYY-MM-DD) to International (DD-MM-YYYY).  For example, JTOD("2025-10-26") would return “26-10-2025”.
250+
* **dtoj():** Converts the International (DD-MM-YYYY) date to japan style of date (YYYY-MM-DD).
251+
252+
The native time format is the HH:MM:SS.FFF. As the FBASIC does not support date or time as datatype, the functions will consider any valid value having this format as a time. Also the functions will consider valid a time of the formats:  HH:MM:SS, HH:MM, and the HH:MM:SS.FFF, HH:MM:SS.FF and HH:MM:SS.F
253+
254+
* **time():** This function returns a string representing the current system time. The format is usually HH:MM:SS.
255+
* **hour():** extracts the hours as an integer from a time variable.
256+
* **minute():** extracts the minutes as an integer from a time variable.
257+
* **second():** extracts the seconds as an integer from a time variable.
225258
226259
### Variables
227260
@@ -320,13 +353,11 @@ assert 9 = 3 ^ 2</code></pre></blockquote></td></tr><tr><td>Arithmetic Operation
320353

321354
Libraries are add-ons to the main FBASIC Interpreter that provide **functions**, **statements**, and **variables** (or **constants**). You can only enable a library during the interpreter's initialization, before you run the FBASIC program. Implementing a library is incredibly easy; refer to the how-to manual for more information.
322355

323-
**Strings Library**
324-
325-
The name of this library is: _FBasicStringFunctions_ and for further information see the _Functions_ section of this document. 
326-
327-
**Mathimatical Library**
328-
329-
The name of this library is: _FBasicMathFunctions_ and for further information see the _Functions_ section of this document. 
356+
* **Buildins Libray:** contains the very basic functions are available to the FBASIC programing language, without the need to enable it. There is switch that the developer can disable that code.
357+
* **Buildins Collections Library:**  contains the very basic functions and statements for collections manipulation. It is enabled by default but the developer can disable that code.
358+
* **Strings Library:** The name of this library is: _FBasicStringFunctions_ and for further information see the _Functions_ section of this document.
359+
* **Mathematical Library:** The name of this library is: _FBasicMathFunctions_ and for further information see the _Functions_ section of this document.
360+
* **Date and Time Library:** The name of this library is: _FBasicDateFunctions_ and for further information see the _Functions_ section of this document.
330361

331362
### SQL Data Provider
332363

@@ -429,4 +460,4 @@ To get the connection string, make a request to the “Request For Object” han
429460
430461
Notes:
431462
432-
> The editor of this file is: https://onlinemarkdowneditor.dev/
463+
> The editor of this file is: https://onlinemarkdowneditor.dev/

FAST.FBasicInterpreter/FAST.FBasicInterpreter.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageReadmeFile>README.md</PackageReadmeFile>
1212
<PackageOutputPath>.\Packages</PackageOutputPath>
1313
<PackageId>FAST.FBasicInterpreter</PackageId>
14-
<Version>1.0.1</Version>
14+
<Version>1.0.2</Version>
1515
<Authors>GCS</Authors>
1616
<RepositoryUrl>https://github.com/aafent/FAST.FBasic</RepositoryUrl>
1717
<PackageProjectUrl>https://github.com/aafent/FAST.FBasic/blob/main/README.md</PackageProjectUrl>

0 commit comments

Comments
 (0)