Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ internal object CppSupportNetwork : SupportNetwork {
put("core.empty()", Like.core("empty"))
put("core.ignore()", Like.ignoring(theLastArg))
put("core.type Boolean.toString()", Like.core("Boolean::toString"))
put("std/temporal.type Date.day", Like.core("Date::getDay"))
put("std/temporal.type Date.month", Like.core("Date::getMonth"))
put("std/temporal.type Date.year", Like.core("Date::getYear"))
put("std/temporal.type Date.get day()", Like.core("Date::getDay"))
put("std/temporal.type Date.get month()", Like.core("Date::getMonth"))
put("std/temporal.type Date.get year()", Like.core("Date::getYear"))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearer intent this way.

put("std/temporal.type Date.get dayOfWeek()", Like.core("Date::getDayOfWeek"))
put("std/temporal.type Date.toString()", Like.core("Date::toString"))
put("std/temporal.type Date.yearsBetween()", Like.core("Date::yearsBetween"))
Expand Down
40 changes: 20 additions & 20 deletions be-cpp/src/commonMain/resources/lang/temper/be/cpp/core/date.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ namespace temper {

template<class D>
int32_t getYear(const std::shared_ptr<D>& d) {
return d->year;
return d->_year;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual fields have uglier names now.

And be-cpp currently makes public fields. We shouldn't do that. But even if we keep this organization the same, we could friend this helper code here for access.


template<class D>
int32_t getMonth(const std::shared_ptr<D>& d) {
return d->month;
return d->_month;
}

template<class D>
int32_t getDay(const std::shared_ptr<D>& d) {
return d->day;
return d->_day;
}

template<class D>
int32_t getDayOfWeek(const std::shared_ptr<D>& d) {
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
int64_t y = d->year;
int64_t m = d->month;
int64_t y = d->_year;
int64_t m = d->_month;
if (m < 1 || m > 12) {
bubble<int32_t>("month out of range");
}
if (m < 3) {
y = y - 1;
}
int64_t dow = (y + y/4 - y/100 + y/400 + t[m-1] + d->day) % 7;
int64_t dow = (y + y/4 - y/100 + y/400 + t[m-1] + d->_day) % 7;
if (dow < 0) {
dow = dow + 7;
}
Expand All @@ -50,13 +50,13 @@ namespace temper {
std::string toString(
const std::shared_ptr<D>& d,
typename std::enable_if<
std::is_same<decltype(std::declval<D>().year), int32_t>::value
&& std::is_same<decltype(std::declval<D>().month), int32_t>::value
&& std::is_same<decltype(std::declval<D>().day), int32_t>::value
std::is_same<decltype(std::declval<D>()._year), int32_t>::value
&& std::is_same<decltype(std::declval<D>()._month), int32_t>::value
&& std::is_same<decltype(std::declval<D>()._day), int32_t>::value
>::type* = nullptr
) {
std::ostringstream oss;
int32_t y = d->year;
int32_t y = d->_year;
if (y < 0) {
oss << '-'; y = -y;
}
Expand All @@ -68,22 +68,22 @@ namespace temper {
oss << "0";
}
oss << y << '-';
if (d->month < 10) {
if (d->_month < 10) {
oss << '0';
}
oss << d->month << '-';
if (d->day < 10) {
oss << d->_month << '-';
if (d->_day < 10) {
oss << '0';
}
oss << d->day;
oss << d->_day;
return oss.str();
}

template<class D>
int32_t yearsBetween(const std::shared_ptr<D>& from, const std::shared_ptr<D>& to) {
int32_t years = to->year - from->year;
if (to->month < from->month
|| (to->month == from->month && to->day < from->day)) {
int32_t years = to->_year - from->_year;
if (to->_month < from->_month
|| (to->_month == from->_month && to->_day < from->_day)) {
years = years - 1;
}
return years;
Expand All @@ -92,9 +92,9 @@ namespace temper {
template<class D>
std::shared_ptr<D> makeDate(int32_t year, int32_t month, int32_t day) {
std::shared_ptr<D> d = std::make_shared<D>();
d->year = year;
d->month = month;
d->day = day;
d->_year = year;
d->_month = month;
d->_day = day;
return d;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1364,13 +1364,13 @@ private val regexFormatterPushCodeTo = StaticCall(
)

private val dateConstructor = ObjectCreation("std/temporal.type Date.constructor()", StandardNames.systemDateTime)
private val dateGetDay = PropertyAccess("std/temporal.type Date.day", "Day")
private val dateGetDay = PropertyAccess("std/temporal.type Date.get day()", "Day")
private val dateGetDayOfWeek = StaticCall(
"std/temporal.type Date.get dayOfWeek()",
StandardNames.temperStdTemporalTemporalSupportIsoWeekdayNum,
)
private val dateGetMonth = PropertyAccess("std/temporal.type Date.month", "Month")
private val dateGetYear = PropertyAccess("std/temporal.type Date.year", "Year")
private val dateGetMonth = PropertyAccess("std/temporal.type Date.get month()", "Month")
private val dateGetYear = PropertyAccess("std/temporal.type Date.get year()", "Year")
private val dateToString = MethodCall(
baseName = "std/temporal.type Date.toString()",
memberNameId = "ToString",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,20 +975,20 @@ val JavaLang.dateToString by receiver {
}
}
val JavaLang.dateGetYear by receiver {
inlineSupport("std/temporal.type Date.year", arity = 1, needsSelf = true) { pos, args ->
inlineSupport("std/temporal.type Date.get year()", arity = 1, needsSelf = true) { pos, args ->
// LocalDate.getYear returns a proleptic year. 2 BC and before are negative.
args[0].method("getYear", pos = pos)
}
}
val JavaLang.dateGetMonth by receiver {
inlineSupport("std/temporal.type Date.month", arity = 1, needsSelf = true) { pos, args ->
inlineSupport("std/temporal.type Date.get month()", arity = 1, needsSelf = true) { pos, args ->
// LocalDate.getMonth returns an instance of the Month enumeration
// .getMonthValue returns an int.
args[0].method("getMonthValue", pos = pos)
}
}
val JavaLang.dateGetDay by receiver {
inlineSupport("std/temporal.type Date.day", arity = 1, needsSelf = true) { pos, args ->
inlineSupport("std/temporal.type Date.get day()", arity = 1, needsSelf = true) { pos, args ->
args[0].method("getDayOfMonth", pos = pos)
}
}
Expand Down Expand Up @@ -1439,10 +1439,10 @@ private val connections: Map<String, ((JavaLang) -> SupportCode)> = mapOf(
// "core.type Console.log()" to null,
"std/temporal.type Date.constructor()" to { it.dateConstructor },
"std/temporal.type Date.fromIsoString()" to { it.dateFromIsoString },
"std/temporal.type Date.day" to { it.dateGetDay },
"std/temporal.type Date.get day()" to { it.dateGetDay },
"std/temporal.type Date.get dayOfWeek()" to { it.dateGetDayOfWeek },
"std/temporal.type Date.month" to { it.dateGetMonth },
"std/temporal.type Date.year" to { it.dateGetYear },
"std/temporal.type Date.get month()" to { it.dateGetMonth },
"std/temporal.type Date.get year()" to { it.dateGetYear },
"std/temporal.type Date.toString()" to { it.dateToString },
"std/temporal.type Date.today()" to { it.dateToday },
"std/temporal.type Date.yearsBetween()" to { it.dateYearsBetween },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ internal object JsSupportNetwork : SupportNetwork {
"std/temporal.type Date.constructor()" -> { p, args, strict, translator ->
newDateIdiomExpander(p, args, strict = strict, genre = genre, translator = translator)
}
"std/temporal.type Date.year" -> propertyReadToMethodCall("getUTCFullYear")
"std/temporal.type Date.month" -> dateGetMonthExpander // Need to add one to index
"std/temporal.type Date.day" -> propertyReadToMethodCall("getUTCDate") // getUTCDay is weekday
"std/temporal.type Date.get year()" -> propertyReadToMethodCall("getUTCFullYear")
"std/temporal.type Date.get month()" -> dateGetMonthExpander // Need to add one to index
"std/temporal.type Date.get day()" -> propertyReadToMethodCall("getUTCDate") // getUTCDay is weekday
"std/temporal.type Date.get dayOfWeek()" -> dateGetDayOfWeekExpander // JS has Sunday as 0, not 7
"std/temporal.type Date.toString()" -> dateToIsoStringExpander
"std/temporal.type Date.fromIsoString()" -> { p, args, strict, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ val DictType = PySeparateCode("dict", SYS_BUILTINS)
val MappingProxyType = PySeparateCode("MappingProxyType", SYS_TYPES)
val DateConstructor = PySeparateCode("date", DATE_TIME)
val DateType = PyConnectedType("date", DATE_TIME)
val DateGetDay = inlineAttribute("std/temporal.type Date.day", PyIdentifierName("day"))
val DateGetMonth = inlineAttribute("std/temporal.type Date.month", PyIdentifierName("month"))
val DateGetYear = inlineAttribute("std/temporal.type Date.year", PyIdentifierName("year"))
val DateGetDay = inlineAttribute("std/temporal.type Date.get day()", PyIdentifierName("day"))
val DateGetMonth = inlineAttribute("std/temporal.type Date.get month()", PyIdentifierName("month"))
val DateGetYear = inlineAttribute("std/temporal.type Date.get year()", PyIdentifierName("year"))
val DateGetDayOfWeek = PyInlineSupportCode(
"std/temporal.type Date.get dayOfWeek()",
arity = 1,
Expand Down Expand Up @@ -1068,10 +1068,10 @@ private val pyConnections = mapOf(
"core.type Boolean.toString()" to BooleanToString,
"std/temporal.type Date.constructor()" to DateConstructor,
"std/temporal.type Date.fromIsoString()" to DateFromIsoString,
"std/temporal.type Date.day" to DateGetDay,
"std/temporal.type Date.get day()" to DateGetDay,
"std/temporal.type Date.get dayOfWeek()" to DateGetDayOfWeek,
"std/temporal.type Date.month" to DateGetMonth,
"std/temporal.type Date.year" to DateGetYear,
"std/temporal.type Date.get month()" to DateGetMonth,
"std/temporal.type Date.get year()" to DateGetYear,
"std/temporal.type Date.toString()" to DateToString,
"std/temporal.type Date.today()" to DateToday,
"std/temporal.type Date.yearsBetween()" to DateYearsBetween,
Expand Down
8 changes: 4 additions & 4 deletions be/src/commonMain/kotlin/lang/temper/be/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The set of *\@connected* methods from Temper's *ImplicitsModule* are:
- `core.type Float64.toString()`
- `core.type Generator`
- `core.type Generator.close()`
- `core.type Generator.done`
- `core.type Generator.get done()`
- `core.type Generator.next()`
- `core.type GeneratorResult`
- `core.type GlobalConsole.globalLog()`
Expand Down Expand Up @@ -211,13 +211,13 @@ to implement:
- `std/regex.type RegexFormatter.regexFormat()`
- `std/temporal.type Date`
- `std/temporal.type Date.constructor()`
- `std/temporal.type Date.day`
- `std/temporal.type Date.fromIsoString()`
- `std/temporal.type Date.get day()`
- `std/temporal.type Date.get dayOfWeek()`
- `std/temporal.type Date.month`
- `std/temporal.type Date.get month()`
- `std/temporal.type Date.get year()`
- `std/temporal.type Date.toString()`
- `std/temporal.type Date.today()`
- `std/temporal.type Date.year`
- `std/temporal.type Date.yearsBetween()`
- `std/testing.processTestCases()`
- `std/testing.reportTestResults()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ internal object TranslateDotHelper {

internal fun connectedKeyForMember(member: MemberShape): String? = when (member) {
is MethodShape -> connectedKeyForMethod(member)
is PropertyShape -> member.connectedKey
is PropertyShape -> null // connect only through accessors methods for now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frontend things should go along with this, but that's TODO in this PR. This tmpl restriction at least limits backends from connecting properties themselves until we work more details out in the future.

is VisibleMemberShape -> member.connectedKey
else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class DateTodayFn(val sig: Signature2) : CallableValue, Stayless {
val propertyName = propertyShape.name as ModularName
val propertyParsedName = (propertyName as ResolvedParsedName).baseName
val value = when (propertyParsedName.nameText) {
"year" -> Value(today.year, TInt)
"month" -> Value(today.monthNumber, TInt)
"day" -> Value(today.dayOfMonth, TInt)
"_year" -> Value(today.year, TInt)
"_month" -> Value(today.monthNumber, TInt)
"_day" -> Value(today.dayOfMonth, TInt)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interpreter also has to use the private names.

else -> error(propertyName)
}
properties[propertyName] = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export interface Generator<@out YIELD extends AnyValue> {
* would get [snippet/builtin/true].
*/
@connected
public done: Boolean;
public get done(): Boolean;
/**
* Should be called once it is realized that
* [snippet/type/Generator/method/next] is not going to be called
Expand Down
21 changes: 12 additions & 9 deletions frontend/src/commonMain/resources/std/temporal/temporal.temper.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,25 @@ Here's just enough of a Date type to get us started.
* A Date identifies a day in the proleptic Gregorian calendar.
* It is unconnected to a time of day or a timezone.
*/
@imu
@json
@connected @imu @json
@imu @json @connected
export class Date {
/** The year. 1900 means 1900. */
@connected
public year: Int;
public get year(): Int { _year }
private _year: Int;

/** The month of the year in [1, 12]. */
@connected
public month: Int;
public get month(): Int { _month }
private _month: Int;

/**
* The day of the month in [1, 31]
* additionally constrained by the length of [month].
*/
@connected
public day: Int;
public get day(): Int { _day }
private _day: Int;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now all getters with private still-imu fields. Key is that the connection is specifically on the getters now.


@connected
public constructor(year: Int, month: Int, day: Int): Void throws Bubble {
Expand All @@ -116,9 +119,9 @@ Here's just enough of a Date type to get us started.
} else {
isLeapYear(year)
})) {
this.year = year;
this.month = month;
this.day = day;
this._year = year;
this._month = month;
this._day = day;
} else {
bubble();
}
Expand Down