Skip to content

Commit

Permalink
Working on Prelude.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofferrose committed Dec 4, 2014
1 parent 670e94f commit c1b62bf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 30 deletions.
4 changes: 1 addition & 3 deletions doc/hacs.tex
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,6 @@ \section{Full Syntax-Directed Definitions}
which now explicitly declares that the new expression should keep all synthetic attributes (again
named with a meta-variable so several sets can be preserved by a single rule).
\item Rule \thetag{1} is slightly more complicated, because the inherited attribute has non-trivial
dependencies. We must know the dependency relationship of the attributes to devise a
\emph{recursive strategy} for the attribute evaluation. Recall that we have the following
Expand Down Expand Up @@ -1258,7 +1256,7 @@ \section{Working with Data}
\section{Working with Numbers}
\label{sec:numbers}
\textbf{To Be Done...}
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ $(BUILD)/org/crsx/hacs/Hacs_symbols.c: $(BUILD)/org/crsx/hacs/Hacs.dr.gz
$(BUILD)/org/crsx/hacs/Hacs.c: crsx-rulecompiler/net/sf/crsx/compiler/c/main.c
cp crsx-rulecompiler/net/sf/crsx/compiler/c/main.c $(BUILD)/org/crsx/hacs/Hacs.c

$(BUILD)/org/crsx/hacs/Hacs.dr.gz: $(BUILD)/org/crsx/hacs/Hacs.crs $(BUILD)/lib/hacs/crsxc
$(BUILD)/org/crsx/hacs/Hacs.dr.gz: $(BUILD)/org/crsx/hacs/Hacs.crs $(BUILD)/lib/hacs/crsxc $(addprefix $(BUILD)/,$(RULES))

all-debug :: $(BUILD)/org/crsx/hacs/Hacs.crsd

Expand Down
12 changes: 11 additions & 1 deletion src/org/crsx/hacs/Prelude.crs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Prelude[(
// Contents:
// 1. Blocking
// 2. Boolean
// 3. Numeric
// 4. List
// 5. String
// 6. Set of Names
Expand Down Expand Up @@ -269,7 +270,7 @@ FilterWhen[ok n.#[ok,n], (#x;#xs)] → Filter2[#[OK,#x], #x, FilterWhen[ok n.#[o
/////////////////////////////////////////////////////////////////////
// 5. STRING

// Note: Uses the internal $String - use Text for all user textual data.
// Note: Uses the internal $String - use Text for user textual data.

// Conversions.

Expand All @@ -282,6 +283,15 @@ UnString[$String] :: $String ;
Trim[$String] :: $String ;
-[Data[#]]: Trim[#] → $[Trim, #] ;

Ord[$String] :: $String ;
-[Data[#]]: Ord[#] → $[DecodePoint, #] ;

Escape[$String] :: $String ;
-[Data[#]]: Escape[#] → $[Escape, #] ;

Unescape[$String] :: $String ;
-[Data[#]]: Unescape[#] → $[Rescape, #] ;

// Tests.

SameString[$String, $String] :: Boolean ;
Expand Down
46 changes: 26 additions & 20 deletions src/org/crsx/hacs/Prelude.pg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@ class org.crsx.hacs.Prelude : <ComputedInteger>

prefix "prelude_"

<ComputedInteger> ::= <ComputedIntegerAdd> .
<ComputedInteger> ::= <ComputedIntegerRel> .

// Pattern expressions (tests).

<ComputedIntegerRel> ::= <ComputedIntegerAdd>:#1
( "=" <ComputedIntegerPrim>:#2 {{ $["NumericEqual", #1, #2] }}
| ("!="|"/="|"≠") <ComputedIntegerPrim>:#2 {{ $["NumericNotEqual", #1, #2] }}
| "<" <ComputedIntegerPrim>:#2 {{ $["LessThan", #1, #2] }}
| ">" <ComputedIntegerPrim>:#2 {{ $["GreaterThan", #1, #2] }}
| ("<="|"≤") <ComputedIntegerPrim>:#2 {{ $["LessThatOrEqual", #1, #2] }}
| (">="|"≥") <ComputedIntegerPrim>:#2 {{ $["GreaterThanOrEqual", #1, #2] }}
| {{#1}}
).

// Expressions.

<ComputedIntegerAdd> ::= <ComputedIntegerMult>:#1 <ComputedIntegerAdd2 #1> .
<ComputedIntegerAdd2 #1> ::=
Expand All @@ -23,45 +37,37 @@ prefix "prelude_"
| ("/"|"÷") <ComputedIntegerPre>:#2 <ComputedIntegerMult2 {{Div[#1,#2]}}>
| "%" <ComputedIntegerPre>:#2 <ComputedIntegerMult2 {{Mod[#1,#2]}}>
| "&" <ComputedIntegerPre>:#2 <ComputedIntegerMult2 {{BitAnd[#1,#2]}}>
| ("<<"|"«") <ComputedIntegerPre>:#2 <ComputedIntegerMult2 {{BitShiftLeft[#1,#2]}}>
| (">>"|"»") <ComputedIntegerPre>:#2 <ComputedIntegerMult2 {{BitShiftRight[#1,#2]}}>
| "<<" <ComputedIntegerPre>:#2 <ComputedIntegerMult2 {{BitShiftLeft[#1,#2]}}>
| ">>" <ComputedIntegerPre>:#2 <ComputedIntegerMult2 {{BitShiftRight[#1,#2]}}>
| {{#1}}
.

<ComputedIntegerPre> ::= <ComputedIntegerPrim>
| "-"{Neg} <ComputedIntegerPre>
| "+" <ComputedIntegerPre>
| "~"{BitNot} <ComputedIntegerPre>
| "ord" {Ord} <ComputedIntegerPrim>
| "tail" {Tail} <ComputedIntegerPrim>
| "escape" {Escape} <ComputedIntegerPrim>
| "unescape" {Unescape} <ComputedIntegerPrim>
.

<ComputedIntegerPrim> ::= "(" <ComputedInteger> ")"
| HEX:# {{$[Hex,#]}}
| DECIMAL:# {{$[Decimal,#]}}
| STRING$
| META@
| "$" {StringToNumeric} <ComputedIntegerRef>
| <ComputedIntegerRef>
.

/// <TestInteger> ::=
///
/// <TestIntegerOr> ::= <TestIntegerOr>:# ( ("∨"|"||"){POr} {{#}} <TestIntegerAnd> | {{#}} ) .
///
/// <TestIntegerAnd> ::= <TestIntegerAnd>:# ( ("∧"|"&&"){PAnd} {{#}} <TestIntegerRel> | {{#}} ) .
/// <TestIntegerRel> ::= <TestIntegerAdd>:#
/// ( "="{PEq} {{#}} <TestIntegerAdd>
/// | ("!="|"≠"){PNeq} {{#}} <TestIntegerAdd>
/// | "<"{PLess} {{#}} <TestIntegerAdd>
/// | ">"{PLess} <TestIntegerAdd> {{#}}
/// | ("<="|"≤"){PLessEqual} <TestIntegerAdd> {{#}}
/// | (">="|"≥"){PLessEqual} {{#}} <TestIntegerAdd>
/// | {{#}}
/// ).
<ComputedIntegerRef> ::= META@ .

token HEX ::= "0x" [0-9A-fa-f]+ .
token DECIMAL ::= [0-9]+ .

token META ::= '#' NAME .

token STRING ::= '"' ( ~[\\\"\n] | [\\] ESCAPEDCHAR )* '"' .
token STRING ::= '"' ( ~[\\""\n] | [\\] ESCAPEDCHAR )* '"' .
fragment ESCAPEDCHAR ::= ~[0-9a-zA-Z] | [\\] [0-3]? [0-7]? [0-7] | [\\] [uU] ( HEX | '{' HEX '}' ) .

token NAME ::= [A-Za-z] [A-Za-z0-9_]* .
19 changes: 14 additions & 5 deletions src/org/crsx/hacs/Util.crs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ EmptyName2ScopeSorts → {}N2SS ;
// Basic ops.

U-ExtendDeclarationMap[DeclarationMap, $String<!--Key-->, DeclarationMapEntry, dm¹::DeclarationMap.DeclarationMap] :: DeclarationMap ;
U-ExtendDeclarationMap[{#DM} DM, #Key, #Entry, dm¹.#[dm¹]] → #[{#DM; #Key : #Entry} DM] ;
U-ExtendDeclarationMap[{#DM} DM, #Key, #Entry, dm¹.#[dm¹]] → $[Trace, $[:,"ExtendDM: ",#Key], #[{#DM; #Key : #Entry} DM]] ;

U-then-ExtendDeclarationMap[Ok, DeclarationMap, $String<!--Key-->, DeclarationMapEntry, dm¹::DeclarationMap.DeclarationMap] :: DeclarationMap ;
U-then-ExtendDeclarationMap[OK, #DM, #Key, #Entry, dm¹.#[dm¹]] → U-ExtendDeclarationMap[#DM, #Key, #Entry, dm¹.#[dm¹]] ;
Expand Down Expand Up @@ -463,7 +463,8 @@ U-MakeDM[{}DM, #Declarations, #ModuleName, "", NoNames, dm¹ s ss.U-MakeDM-Finis

{#DM} U-MakeDM1[%rawHxDeclarations⟪ main sort #HxSortName# #HxRepeat# #HxSortAbstraction# ; #HxDeclarations# ⟫, #ModuleName, #LastSortName, #Names, dm¹ s ss.#[dm¹, s, ss]]
{#DM} U-MakeDM-SortAbstraction[#HxSortAbstraction#, B_TRUE, #ModuleName, U-SortNameWithRepeat1[#HxSortName#, #HxRepeat#], #Names, dm¹ s ss.U-MakeDM[dm¹, #HxDeclarations#, #ModuleName, s, ss, dm2¹ s2 ss2.#[dm2¹, s2, ss2]]]
{#DM} U-MakeDM-SortAbstraction[#HxSortAbstraction#, B_TRUE, #ModuleName, U-QN[#ModuleName, "", U-SortNameWithRepeat1[#HxSortName#, #HxRepeat#]], #Names,
dm¹ s ss.U-MakeDM[dm¹, #HxDeclarations#, #ModuleName, s, ss, dm2¹ s2 ss2.#[dm2¹, s2, ss2]]]
;

{#DM} U-MakeDM1[%rawHxDeclarations⟪ #HxSortAlternatives# ; #HxDeclarations# ⟫, #ModuleName, #LastSortName, #Names, dm¹ s ss.#[dm¹, s, ss]]
Expand Down Expand Up @@ -495,7 +496,9 @@ U-MakeDM[{}DM, #Declarations, #ModuleName, "", NoNames, dm¹ s ss.U-MakeDM-Finis
{#DM}
U-MakeDM2[#Key, #Entry, #Declarations, #ModuleName, #LastSortName, #Names, dm¹ s ss.#[dm¹, s, ss]]
$[Trace, $[:, "DM2-Extend: ",#Key],
{#DM; #Key : #Entry} U-MakeDM1[#Declarations, #ModuleName, #LastSortName, #Names, dm¹ s ss.#[dm¹, s, ss]]
]
;

// Sort declarations factor any leading parameters over all declarations.
Expand Down Expand Up @@ -637,7 +640,6 @@ U-MakeDM[{}DM, #Declarations, #ModuleName, "", NoNames, dm¹ s ss.U-MakeDM-Finis
-[Data[#Prec,#Precs,#MaxPrec]]:
{#DM} U-MakeDM-Finish6[#ModuleName, #Prec, #Precs, #MaxPrec, #Name, #RestNames, #Names]
///{#DM} U-MakeDM-Finish4[#ModuleName, #Precs, $[If, $[GreaterThan, $[Decimal,#Prec], #MaxPrec], $[Decimal,#Prec], #MaxPrec], #Name, #RestNames, #Names]
{#DM} U-MakeDM-Finish4[#ModuleName, #Precs, Max2[#Prec, #MaxPrec], #Name, #RestNames, #Names]
;

Expand Down Expand Up @@ -702,8 +704,10 @@ U-MakeDM[{}DM, #Declarations, #ModuleName, "", NoNames, dm¹ s ss.U-MakeDM-Finis
U-InsertDefinesMain1[$List[Define], $String<!--Name-->, NameSet<!--Precs-->, Wrapper, Boolean<!--IsMain-->, $String<!--Name-->, NameSet<!--Names-->, dm¹ :: DeclarationMap s :: $String ss :: NameSet . DeclarationMap] :: DeclarationMap ;
-[Data[#IsMain]]:
{#DM} U-InsertDefinesMain1[#existingDefines1, #Name1, #Precs1, #Wrapper1, #IsMain, #Name, #Names, dm¹ s ss.#[dm¹, s, ss]]
$[Trace, $[:,"InsertSortMain1: ",#Name],
#[{#DM; #Name : DME_SORT[#existingDefines1, #Name1, #Precs1, #Wrapper1, #IsMain]}DM, #Name, #Names]
]
;

{#DM} U-InsertDefinesMain[$[NotMatch,DME_SORT[#existingDefines1, #Name1, #Precs1, #Wrapper1, #IsMain1],#_], #IsMain, #Name, #Names, dm¹ s ss.#[dm¹, s, ss]]
Expand Down Expand Up @@ -771,7 +775,9 @@ U-MakeDM[{}DM, #Declarations, #ModuleName, "", NoNames, dm¹ s ss.U-MakeDM-Finis
{#DM}
U-InsertDefine1b[#Name, #Names, #newDefine, #Precs, #Wrapper, #IsMain, dm¹ s ss.#[dm¹, s, ss]]
$[Trace, $[:,"InsertSort1b: ",#Name],
#[{#DM; #Name : DME_SORT[(#newDefine;), #Name, #Precs, #Wrapper, #IsMain]}DM, #Name, AddName[#Names, #Name]]
]
;

U-InsertDefine2[$List[Define]<!--existing-->, Define<!--new-->] :: $List[Define]<!--merged-->;
Expand All @@ -796,7 +802,9 @@ U-MakeDM[{}DM, #Declarations, #ModuleName, "", NoNames, dm¹ s ss.U-MakeDM-Finis
{#DM}
U-InsertDefine4[#Defines, #Precs, #Name, #Names, #Wrapper, #IsMain, dm¹ s ss.#[dm¹, s, ss]]
$[Trace, $[:,"InsertSort4; ",#Name],
#[{#DM; #Name : DME_SORT[#Defines, #Name, #Precs, #Wrapper, #IsMain]}DM, #Name, #Names]
]
;

// Compare of defines.
Expand Down Expand Up @@ -1009,7 +1017,8 @@ U-MakeDM-ExtraInformation1[#ModuleName, {#DM}DM]
// Generate the sort names.

{$String<!--Name--> : DeclarationMapEntry} U-MakeSortNames :: NameSet;
{#DM} U-MakeSortNames → NamesFrom[FilterWhen[ok n . {#DM}U-then-TestIfSort[ok, n], $[{#DM}Keys[$String]]]] ;
{#DM} U-MakeSortNames → NamesFrom[FilterWhen[ok n . {#DM}U-then-TestIfSort[ok, n], $[{#DM}Keys[$String]]]] ;
/// {#DM} U-MakeSortNames → NamesFrom[FilterWhen[ok n . TraceWhen[ok, "MakeSort: ", n, {#DM}U-then-TestIfSort[ok, n]], $[{#DM}Keys[$String]]]] ;
/// FilterNamesWhen[ok n.{#DM} U-then-TestIfSort[ok, n], NamesFrom[$[{#DM}Keys[$String]]]] ;

{$String<!--Name--> : DeclarationMapEntry} U-then-TestIfSort[Ok, $String] :: Boolean ;
Expand Down

0 comments on commit c1b62bf

Please sign in to comment.