Skip to content
This repository was archived by the owner on Dec 29, 2018. It is now read-only.
Open
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
83 changes: 65 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ specifiers are defined as follows:
| and-pattern
| constructor-pattern
| derived-pattern

constant-pattern ::= t | nil
| keyword
| atom-except-symbol
| (quote VALUE)

variable-pattern ::= SYMBOL | (variable SYMBOL)

place-pattern ::= (place SYMBOL)

guard-pattern ::= (guard PATTERN TEST-FORM)

not-pattern ::= (not PATTERN)

or-pattern ::= (or PATTERN*)

and-pattern ::= (and PATTERN*)

constructor-pattern ::= (NAME ARG*)

derived-pattern ::= (NAME PATTERN*)

### Constant-Pattern
Expand Down Expand Up @@ -205,6 +205,54 @@ Examples:
((simple-vector a b) (+ a b)))
=> 3

#### VECTOR*

Syntax:

vector*-constructor-pattern ::= (vector* PATTERN+)

Examples:

(match #(1 2 3)
((vector* a b) (list a b)))
=> (1 #(2 3))

#### SIMPLE-VECTOR*

Syntax:

simple-vector*-constructor-pattern ::= (simple-vector* PATTERN+)

Examples:

(match #(1 2 3)
((simple-vector* a b) (list a b)))
=> (1 #(2 3))

#### SEQUENCE

Syntax:

sequence-constructor-pattern ::= (sequence PATTERN*)

Examples:

(match "abc"
((sequence a b c) (list a b c)))
=> (#\a #\b #\c)

#### SEQUENCE*

Syntax:

sequence*-constructor-pattern ::= (sequence* PATTERN+)

Examples:

(match "abc"
((sequence* a b) (list a b)))
=> (#\a "bc"))

#### CLASS

Matches an instance of a given subclass of standard-class, as well as
Expand All @@ -214,7 +262,7 @@ Syntax:

class-constructor-pattern ::= (class NAME slot*)
| (NAME slot*)

slot ::= SLOT-NAME
| (SLOT-NAME PATTERN*)

Expand Down Expand Up @@ -259,7 +307,7 @@ Syntax:

structure-constructor-pattern ::= (structure CONC-NAME slot*)
| (CONC-NAME slot*)

slot ::= SLOT-NAME
| (SLOT-NAME PATTERN*)

Expand Down Expand Up @@ -304,7 +352,7 @@ style pattern syntax like:
### Derived-Pattern

A derived-pattern is a pattern that is defined with DEFPATTERN. There
are some builtin dervied patterns as below:
are some builtin derived patterns as below:

#### LIST

Expand Down Expand Up @@ -356,7 +404,7 @@ Define Constructor Patterns
---------------------------

You can define your own constructor patterns by using `OPTIMA.CORE`
package. Firstly, define a data structore for the constructor
package. Firstly, define a data structure for the constructor
pattern.

(defstruct (my-cons-pattern (:include constructor-pattern)
Expand All @@ -370,13 +418,12 @@ condition when destructor of the constructor patterns can be shared.
Sharing destructors removes redundant data checks, that is,
pattern-matching can get more faster.


(defmethod constructor-pattern-destructor-sharable-p ((x my-cons-pattern) (y my-cons-pattern))
t)

Thirdly, define a destructor generator for the constructor pattern,
whichs generate a destructor that specifies how to check the the
data (`PREDICATE-FORM`) and how to access the data (`ACCESSOR-FORMS`).
which generates a destructor that specifies how to check the data
(`PREDICATE-FORM`) and how to access the data (`ACCESSOR-FORMS`).

(defmethod constructor-pattern-make-destructor ((pattern my-cons-pattern) var)
(make-destructor :predicate-form `(consp ,var)
Expand All @@ -386,7 +433,7 @@ Finally, define a parser and an unparser for the constructor pattern.

(defmethod parse-constructor-pattern ((name (eql 'my-cons)) &rest args)
(apply #'make-my-cons-pattern (mapcar #'parse-pattern args)))

(defmethod unparse-pattern ((pattern my-cons-pattern))
`(cons ,(unparse-pattern (my-cons-pattern-car-pattern pattern))
,(unparse-pattern (my-cons-pattern-cdr-pattern pattern))))
Expand All @@ -400,7 +447,7 @@ See the source code for more detail.

%equal a b

Equality function for comparing patten constants.
Equality function for comparing pattern constants.

### [Macro] %equals

Expand Down
78 changes: 63 additions & 15 deletions optima.asd
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ specifiers are defined as follows:
| and-pattern
| constructor-pattern
| derived-pattern

constant-pattern ::= t | nil
| keyword
| atom-except-symbol
| (quote VALUE)

variable-pattern ::= SYMBOL | (variable SYMBOL)

place-pattern ::= (place SYMBOL)

guard-pattern ::= (guard PATTERN TEST-FORM)

not-pattern ::= (not PATTERN)

or-pattern ::= (or PATTERN*)

and-pattern ::= (and PATTERN*)

constructor-pattern ::= (NAME ARG*)

derived-pattern ::= (NAME PATTERN*)

### Constant-Pattern
Expand Down Expand Up @@ -204,6 +204,54 @@ Examples:
((simple-vector a b) (+ a b)))
=> 3

#### VECTOR*

Syntax:

vector*-constructor-pattern ::= (vector* PATTERN*)

Examples:

(match #(1 2 3)
((vector* a b) (list a b)))
=> (1 #(2 3))

#### SIMPLE-VECTOR*

Syntax:

simple-vector*-constructor-pattern ::= (simple-vector* PATTERN*)

Examples:

(match #(1 2 3)
((simple-vector* a b) (list a b)))
=> (1 #(2 3))

#### SEQUENCE

Syntax:

sequence-constructor-pattern ::= (sequence PATTERN*)

Examples:

(match \"abc\"
((sequence a b c) (list a b c)))
=> (#\\a #\\b #\\c)

#### SEQUENCE*

Syntax:

sequence*-constructor-pattern ::= (sequence* PATTERN*)

Examples:

(match \"abc\"
((sequence* a b) (list a b)))
=> (#\\a \"bc\"))

#### CLASS

Matches an instance of a given subclass of standard-class, as well as
Expand All @@ -213,7 +261,7 @@ Syntax:

class-constructor-pattern ::= (class NAME slot*)
| (NAME slot*)

slot ::= SLOT-NAME
| (SLOT-NAME PATTERN*)

Expand Down Expand Up @@ -258,7 +306,7 @@ Syntax:

structure-constructor-pattern ::= (structure CONC-NAME slot*)
| (CONC-NAME slot*)

slot ::= SLOT-NAME
| (SLOT-NAME PATTERN*)

Expand Down Expand Up @@ -303,7 +351,7 @@ style pattern syntax like:
### Derived-Pattern

A derived-pattern is a pattern that is defined with DEFPATTERN. There
are some builtin dervied patterns as below:
are some builtin derived patterns as below:

#### LIST

Expand Down Expand Up @@ -355,7 +403,7 @@ Define Constructor Patterns
---------------------------

You can define your own constructor patterns by using `OPTIMA.CORE`
package. Firstly, define a data structore for the constructor
package. Firstly, define a data structure for the constructor
pattern.

(defstruct (my-cons-pattern (:include constructor-pattern)
Expand All @@ -374,7 +422,7 @@ pattern-matching can get more faster.
t)

Thirdly, define a destructor generator for the constructor pattern,
whichs generate a destructor that specifies how to check the the
which generate a destructor that specifies how to check the
data (`PREDICATE-FORM`) and how to access the data (`ACCESSOR-FORMS`).

(defmethod constructor-pattern-make-destructor ((pattern my-cons-pattern) var)
Expand All @@ -385,7 +433,7 @@ Finally, define a parser and an unparser for the constructor pattern.

(defmethod parse-constructor-pattern ((name (eql 'my-cons)) &rest args)
(apply #'make-my-cons-pattern (mapcar #'parse-pattern args)))

(defmethod unparse-pattern ((pattern my-cons-pattern))
`(cons ,(unparse-pattern (my-cons-pattern-car-pattern pattern))
,(unparse-pattern (my-cons-pattern-cdr-pattern pattern))))
Expand Down
20 changes: 20 additions & 0 deletions src/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@
#:simple-vector-pattern-subpatterns
#:make-simple-vector-pattern

#:vector*-pattern
#:vector*-pattern-subpatterns
#:make-vector*-pattern

#:simple-vector*-pattern
#:simple-vector*-pattern-subpatterns
#:make-simple-vector*-pattern

#:sequence-pattern
#:sequence-pattern-subpatterns
#:make-sequence-pattern

#:sequence*-pattern
#:sequence*-pattern-subpatterns
#:make-sequence*-pattern

#:class-pattern
#:class-pattern-subpatterns
#:class-pattern-class-name
Expand Down Expand Up @@ -111,6 +127,7 @@
(defpackage :optima
(:use :cl :optima.core)
(:import-from :alexandria
#:length=
#:ensure-car
#:ensure-list
#:mappend
Expand Down Expand Up @@ -142,6 +159,9 @@
#:place
#:guard
#:property
#:vector*
#:simple-vector*
#:sequence*
#:defpattern))

(defpackage :optima.extra
Expand Down
Loading