Skip to content

Commit 722bba3

Browse files
authored
Merge pull request #931 from live-codes/minizinc
add minizinc language support
2 parents 7f5d6aa + 0d35428 commit 722bba3

28 files changed

Lines changed: 1127 additions & 5 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A [feature-rich](https://livecodes.io/docs/features/), open-source, **client-sid
1313
[![LiveCodes: npm version](https://img.shields.io/npm/v/livecodes)](https://www.npmjs.com/package/livecodes)
1414
[![LiveCodes: npm downloads](https://img.shields.io/npm/dm/livecodes)](https://www.npmjs.com/package/livecodes)
1515
[![LiveCodes: jsdelivr downloads](https://data.jsdelivr.com/v1/package/npm/livecodes/badge?style=rounded)](https://www.jsdelivr.com/package/npm/livecodes)
16-
[![LiveCodes: languages](https://img.shields.io/badge/languages-97-blue)](https://livecodes.io/docs/languages/)
16+
[![LiveCodes: languages](https://img.shields.io/badge/languages-98-blue)](https://livecodes.io/docs/languages/)
1717
[![LiveCodes: docs](https://img.shields.io/badge/Documentation-575757?logo=gitbook&logoColor=white)](https://livecodes.io/docs/)
1818
[![LiveCodes: llms.txt](https://img.shields.io/badge/llms.txt-575757?logo=googledocs&logoColor=white)](https://livecodes.io/docs/llms.txt)
1919
[![LiveCodes: llms-full.txt](https://img.shields.io/badge/llms--full.txt-575757?logo=googledocs&logoColor=white)](https://livecodes.io/docs/llms-full.txt)

docs/docs/languages/minizinc.mdx

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# MiniZinc
2+
3+
[MiniZinc](https://www.minizinc.org/) is a high-level constraint modelling language that allows you to easily express and solve discrete optimisation problems.
4+
5+
LiveCodes runs MiniZinc in the browser using WebAssembly.
6+
7+
## Basic Demo
8+
9+
import LiveCodes from '../../src/components/LiveCodes.tsx';
10+
11+
export const params = {
12+
'minizinc': `% Soduku
13+
14+
include "globals.mzn";
15+
16+
any: board = [|
17+
5, 3, <>, <>, 7, <>, <>, <>, <> |
18+
6, <>, <>, 1, 9, 5, <>, <>, <> |
19+
<>, 9, 8, <>, <>, <>, <>, 6, <> |
20+
21+
8, <>, <>, <>, 6, <>, <>, <>, 3 |
22+
4, <>, <>, 8, <>, 3, <>, <>, 1 |
23+
7, <>, <>, <>, 2, <>, <>, <>, 6 |
24+
25+
<>, 6, <>, <>, <>, <>, 2, 8, <> |
26+
<>, <>, <>, 4, 1, 9, <>, <>, 5 |
27+
<>, <>, <>, <>, 8, <>, <>, 7, 9
28+
|];
29+
30+
array [1..9, 1..9] of var 1..9: solution;
31+
32+
% Given numbers are fixed
33+
constraint forall (i, j in 1..9) (solution[i, j] ~= board[i, j]);
34+
35+
% Rows are all different
36+
constraint forall (i in 1..9) (all_different(solution[i, ..]));
37+
% Columns are all different
38+
constraint forall (j in 1..9) (all_different(solution[.., j]));
39+
40+
% Subgrids are all different
41+
constraint forall (i, j in 1..3) (
42+
all_different(solution[
43+
3 * (i - 1) + 1 .. 3 * i,
44+
3 * (j - 1) + 1 .. 3 * j
45+
])
46+
);
47+
48+
solve satisfy;
49+
`,
50+
console: 'full',
51+
};
52+
53+
<LiveCodes params={params}></LiveCodes>
54+
55+
See below for more advanced examples.
56+
57+
## Usage
58+
59+
By default the output is logged to the integrated console.
60+
In addition, helper methods are available to access solve progress and solution from JavaScript.
61+
62+
### Usage from JavaScript
63+
64+
Helper methods are available in the browser global `livecodes.minizinc` object.
65+
They allow interacting with the [JavaScript interface for MiniZinc](https://js.minizinc.dev/).
66+
67+
The following methods are available:
68+
69+
- `livecodes.minizinc.init`: A method that returns a promise that resolves when the MiniZinc environment is loaded. This should be used before calling `livecodes.minizinc.solve`.
70+
- `livecodes.minizinc.getSolvers`: A method that returns a promise that resolves to an array of available MiniZinc solvers.
71+
- `livecodes.minizinc.run`: A method that returns a promise that resolves to the final solution/statistics/status. It optionally accepts a data object (see below) as an argument.
72+
- `livecodes.minizinc.solve`: This method should only be run after `livecodes.minizinc.init()` resolves. It returns a [solve progress object](https://js.minizinc.dev/docs/stable/interfaces/SolveProgress.html), which can be used to listen to events during solving, and can be awaited to retrieve the final solution/statistics/status. This method also optionally accepts a data object (see below) as an argument.
73+
74+
#### Data Object
75+
76+
The data object can be used to pass data to the MiniZinc environment, such as dzn or json.
77+
It can also pass configuration object. It has the following type definition:
78+
79+
```ts
80+
interface MiniZincData {
81+
dzn?: string;
82+
json?: string;
83+
config?: {
84+
jsonOutput?: boolean;
85+
options?: {
86+
solver?: string;
87+
"time-limit"?: number;
88+
statistics?: boolean;
89+
"all-solutions"?: boolean;
90+
// ... other MiniZinc options
91+
};
92+
};
93+
}
94+
```
95+
96+
#### Example
97+
98+
<LiveCodes template='minizinc' height="80vh"></LiveCodes>
99+
100+
## Language Info
101+
102+
### Name
103+
104+
`minizinc`
105+
106+
### Extension
107+
108+
`mzn`
109+
110+
### Editor
111+
112+
`script`
113+
114+
## Compiler
115+
116+
The official [JavaScript port](https://js.minizinc.dev/) for MiniZinc which uses WebAssembly.
117+
118+
### Version
119+
120+
`minizinc` v4.4.4
121+
122+
## Code Formatting
123+
124+
Using a [MiniZinc plugin](https://github.com/live-codes/prettier-plugin-minizinc) for the [Prettier](https://prettier.io/) formatter.
125+
126+
127+
## Limitations
128+
129+
Currently, [visualisations](https://docs.minizinc.dev/en/stable/visualisation.html) are not supported out-of-the-box.
130+
However, using the [helper methods](#usage-from-javascript), and based on [official implementations](https://github.com/MiniZinc/libminizinc/tree/master/share/minizinc/std/ide), it should be possible to create custom visualisations.
131+
132+
Example:
133+
134+
<LiveCodes import='id/xj98m7cfpnv' height="80vh"></LiveCodes>
135+
136+
## Starter Template
137+
138+
https://livecodes.io/?template=minizinc
139+
140+
## Links
141+
142+
- [MiniZinc](https://www.minizinc.org/)
143+
- [MiniZinc documentation](https://docs.minizinc.dev/en/stable/)
144+
- [MiniZinc JavaScript port](https://js.minizinc.dev/)
145+
- [MiniZinc tutorial](https://docs.minizinc.dev/en/stable/part_2_tutorial.html)

docs/docusaurus.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ const config: Config = {
290290
plugin: ['typedoc-plugin-missing-exports'],
291291
excludeExternals: true,
292292
internalModule: '_internal',
293+
skipErrorChecking: true,
293294
},
294295
],
295296
[

docs/src/components/LanguageSliders.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export default function Sliders() {
106106
{ name: 'sql', title: 'SQL' },
107107
{ name: 'postgresql', title: 'PostgreSQL' },
108108
{ name: 'prolog', title: 'Prolog' },
109+
{ name: 'minizinc', title: 'MiniZinc' },
109110
{ name: 'blockly', title: 'Blockly' },
110111
],
111112
};

docs/src/components/TemplateList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const templates = [
6666
{ name: 'sql', title: 'SQL Starter', thumbnail: 'sqlite.svg' },
6767
{ name: 'postgresql', title: 'PostgreSQL Starter', thumbnail: 'postgresql.svg' },
6868
{ name: 'prolog', title: 'Prolog Starter', thumbnail: 'tau-prolog.svg' },
69+
{ name: 'minizinc', title: 'MiniZinc Starter', thumbnail: 'minizinc.png' },
6970
{ name: 'blockly', title: 'Blockly Starter', thumbnail: 'blockly.svg' },
7071
{ name: 'diagrams', title: 'Diagrams Starter', thumbnail: 'diagrams.svg' },
7172
];

scripts/build.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ const esmBuild = () =>
160160
'editor/monaco/languages/monaco-lang-astro.ts',
161161
'editor/monaco/languages/monaco-lang-clio.ts',
162162
'editor/monaco/languages/monaco-lang-imba.ts',
163+
'editor/monaco/languages/monaco-lang-minizinc.ts',
164+
'editor/monaco/languages/monaco-lang-prolog.ts',
163165
// 'editor/monaco/languages/monaco-lang-sql.ts',
164166
'editor/monaco/languages/monaco-lang-wat.ts',
165167
'editor/codemirror/codemirror.ts',
@@ -228,6 +230,7 @@ const iifeBuild = () =>
228230
'languages/liquid/lang-liquid-compiler.ts',
229231
'languages/lua-wasm/lang-lua-wasm-script.ts',
230232
'languages/malina/lang-malina-compiler.ts',
233+
'languages/minizinc/lang-minizinc-script.ts',
231234
'languages/mustache/lang-mustache-compiler.ts',
232235
'languages/nunjucks/lang-nunjucks-compiler.ts',
233236
'languages/perl/lang-perl-script.ts',

src/livecodes/UI/command-menu-actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ export const getCommandMenuActions = ({
323323
'sql',
324324
'postgresql',
325325
'prolog',
326+
'minizinc',
326327
'blockly',
327328
'diagrams',
328329
).map((template) => ({
24.5 KB
Loading

src/livecodes/editor/codemirror/editor-languages.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const moduleUrls = {
3434
sql: getPath('codemirror-lang-sql.js'),
3535
wast: getPath('codemirror-lang-wast.js'),
3636
scss: getPath('codemirror-lang-scss.js'),
37+
minizinc: getPath('codemirror-lang-minizinc.js'),
38+
prolog: getPath('codemirror-lang-prolog.js'),
3739
coffeescript: getPath('codemirror-lang-coffeescript.js'),
3840
livescript: getPath('codemirror-lang-livescript.js'),
3941
ruby: getPath('codemirror-lang-ruby.js'),
@@ -72,6 +74,8 @@ export const editorLanguages: Partial<{ [key in Language]: () => Promise<Languag
7274
wat: async () => (await import(moduleUrls.wast)).wast(),
7375
scss: async () => (await import(moduleUrls.scss)).sass(),
7476
sass: async () => (await import(moduleUrls.scss)).sass({ indented: true }),
77+
minizinc: async () => (await import(moduleUrls.minizinc)).MiniZinc(),
78+
prolog: async () => (await import(moduleUrls.prolog)).prolog(),
7579
coffeescript: async () => legacy((await import(moduleUrls.coffeescript)).coffeeScript),
7680
livescript: async () => legacy((await import(moduleUrls.livescript)).liveScript),
7781
ruby: async () => legacy((await import(moduleUrls.ruby)).ruby),

0 commit comments

Comments
 (0)