Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATE, DATE_AND_TIME, TIME, TIME_OF_DAY #173

Merged
merged 8 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rusty"
version = "0.1.0"
version = "0.2.0"
authors = ["Ghaith Hachem <ghaith.hachem@gmail.com>", "Mathias Rieder <mathias.rieder@gmail.com>"]
edition = "2018"
readme = "README.md"
Expand All @@ -16,6 +16,8 @@ pretty_assertions = "0.6.1"
thiserror = "1.0"
structopt = "0.3"
indexmap = "1.6"
chrono = "0.4"


[lib]
name = "rusty"
Expand Down
2 changes: 2 additions & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
- [Multiple Files]()
- [Libraries]()
- [Using in external programs]()

- [Datatypes](./datatypes.md)
61 changes: 61 additions & 0 deletions book/src/datatypes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Datatypes

## Date and Time
### DATE
The `DATE` datatype is used to represent a Date in the Gregorian Calendar. Such a value is
stored as an i64 with a precision in milliseconds and denotes the number of milliseconds
that have elapsed since January 1, 1970 UTC not counting leap seconds. DATE literals start
with `DATE#` or `D#` followed by a date in the format of `yyyy-mm-dd`.

Example literals
- `d1 : DATE := DATE#2021-05-02;`
- `d2 : DATE := DATE#1-12-24;`
- `d3 : DATE := D#2000-1-1;`

### DATE_AND_TIME
The `DATE_AND_TIME` datatype is used to represent a certain point in time in the Gregorian Calendar.
Such a value is stored as an `i64` with a precision in milliseconds and denotes the
number of milliseconds that have elapsed since January 1, 1970 UTC not counting leap seconds.
DATE_AND_TIME literals start with `DATE_AND_TIME#` or `DT#` followed by a date and time in the
format of `yyyy-mm-dd-hh:mm:ss`.

Note that only the seconds-segment can have a fraction denoting the milliseconds.

Example literals
- `d1 : DATE_AND_TIME := DATE_AND_TIME#2021-05-02-14:20:10.25;`
- `d2 : DATE_AND_TIME := DATE_AND_TIME#1-12-24-00:00:1;`
- `d3 : DATE_AND_TIME := DT#1999-12-31-23:59:59.999;`

### TIME_OF_DAY
The `TIME_OF_DAY` datatype is used to represent a specific moment in time in a day.
Such a value is stored as an `i64` value with a precision in milliseconds and denotes the
number of milliseconds that have elapsed since January 1, 1970 UTC not counting leap seconds.
Hence this value is stored as a `DATE_AND_TIME` with the day fixed to 1970-01-01.
`TIME_OF_DAY` literals start with `TIME_OF_DAY#` or `TOD#` followed by a time in the
format of `hh:mm:ss`.

Note that only the seconeds-segment can have a fraction denoting the milliseconds.

Example literals
- `t1 : TIME_OF_DAY := TIME_OF_DAY#14:20:10.25;`
- `t2 : TIME_OF_DAY := TIME_OF_DY#0:00:1;`
- `t3 : TIME_OF_DAY := TOD#23:59:59.999;`

### TIME
The `TIME` datatype is used to represent a time-span. A `TIME` value is stored as an
`i64` value with a precision in nanoseconds.
TIME literals start with `TIME#` or `T#` followed by the `TIME` segements. Supported segements are:
- `d` ... `f64` days
- `h` ... `f64` hours
- `m` ... `f64`minutes
- `s` ... `f64` seconds
- `ms` ... `f64` milliseconds
- `us` ... `f64` microseconds
- `ns` ... `u32` nanaoseconds

Note that only the last segment of a `TIME` literal can have a fraction.

Example literals
- `t1 : TIME := TIME#2d4h6m8s10ms;`
- `t2 : TIME := T#2d4.2h;`
- `t3 : TIME := T#-10s4ms16ns;`
99 changes: 99 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,40 @@ pub enum Statement {
value: String,
location: SourceRange,
},
LiteralDate {
year: i32,
month: u32,
day: u32,
location: SourceRange,
},
LiteralDateAndTime {
year: i32,
month: u32,
day: u32,
hour: u32,
min: u32,
sec: u32,
milli: u32,
location: SourceRange,
},
LiteralTimeOfDay {
hour: u32,
min: u32,
sec: u32,
milli: u32,
location: SourceRange,
},
LiteralTime {
day: f64,
hour: f64,
min: f64,
sec: f64,
milli: f64,
micro: f64,
nano: u32,
negative: bool,
location: SourceRange,
},
LiteralReal {
value: String,
location: SourceRange,
Expand Down Expand Up @@ -496,6 +530,67 @@ impl Debug for Statement {
.debug_struct("LiteralInteger")
.field("value", value)
.finish(),
Statement::LiteralDate {
year, month, day, ..
} => f
.debug_struct("LiteralDate")
.field("year", year)
.field("month", month)
.field("day", day)
.finish(),
Statement::LiteralDateAndTime {
year,
month,
day,
hour,
min,
sec,
milli,
..
} => f
.debug_struct("LiteralDateAndTime")
.field("year", year)
.field("month", month)
.field("day", day)
.field("hour", hour)
.field("min", min)
.field("sec", sec)
.field("milli", milli)
.finish(),
Statement::LiteralTimeOfDay {
hour,
min,
sec,
milli,
..
} => f
.debug_struct("LiteralTimeOfDay")
.field("hour", hour)
.field("min", min)
.field("sec", sec)
.field("milli", milli)
.finish(),
Statement::LiteralTime {
day,
hour,
min,
sec,
milli,
micro,
nano,
negative,
..
} => f
.debug_struct("LiteralTime")
.field("day", day)
.field("hour", hour)
.field("min", min)
.field("sec", sec)
.field("milli", milli)
.field("micro", micro)
.field("nano", nano)
.field("negative", negative)
.finish(),
Statement::LiteralReal { value, .. } => {
f.debug_struct("LiteralReal").field("value", value).finish()
}
Expand Down Expand Up @@ -642,6 +737,10 @@ impl Statement {
pub fn get_location(&self) -> SourceRange {
match self {
Statement::LiteralInteger { location, .. } => location.clone(),
Statement::LiteralDate { location, .. } => location.clone(),
Statement::LiteralDateAndTime { location, .. } => location.clone(),
Statement::LiteralTimeOfDay { location, .. } => location.clone(),
Statement::LiteralTime { location, .. } => location.clone(),
Statement::LiteralReal { location, .. } => location.clone(),
Statement::LiteralBool { location, .. } => location.clone(),
Statement::LiteralString { location, .. } => location.clone(),
Expand Down
Loading