orvyn-lang 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +158 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,4 +22,161 @@ screen Home {
|
|
|
22
22
|
}
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
|
|
26
|
+
## Declarations (top level)
|
|
27
|
+
|
|
28
|
+
| Syntax | Meaning |
|
|
29
|
+
|---|---|
|
|
30
|
+
| `state name = value` | A value that changes. The screen updates when it changes. |
|
|
31
|
+
| `saved state name = value` | Like `state`, but kept after a reload or app restart (cart, settings, login). |
|
|
32
|
+
| `let name = value` | A value that never changes. |
|
|
33
|
+
| `fn name(a, b) { ... }` | A function. |
|
|
34
|
+
| `screen Name { ... }` | A screen of your app. `App` (or the first screen) is shown first. |
|
|
35
|
+
| `component Name(param) { ... }` | A reusable piece of UI. |
|
|
36
|
+
| `start { ... }` | Runs once when the app opens (load data here). |
|
|
37
|
+
| `use Card, Badge from "./ui"` | Use components or functions from another `.orv` file. |
|
|
38
|
+
|
|
39
|
+
## Statements (inside `fn` and handlers)
|
|
40
|
+
|
|
41
|
+
```orv
|
|
42
|
+
let total = price * 2 // local variable
|
|
43
|
+
total += 1 // also -=, *=, /=
|
|
44
|
+
count = 0 // update state
|
|
45
|
+
if count > 10 { ... } else { ... }
|
|
46
|
+
for item in items { ... }
|
|
47
|
+
return total
|
|
48
|
+
print("hello") // logs to the console
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Lists and objects
|
|
52
|
+
|
|
53
|
+
Change them with these, and the screen updates by itself:
|
|
54
|
+
|
|
55
|
+
```orv
|
|
56
|
+
state todos = []
|
|
57
|
+
state user = { name: "Asha", age: 20 }
|
|
58
|
+
|
|
59
|
+
todos.add({ text: "Milk", done: false }) // add to the end
|
|
60
|
+
todos.remove(todo) // remove this item
|
|
61
|
+
todos.removeAt(0) // remove by position
|
|
62
|
+
todos.update(todo, { done: true }) // change fields of one item
|
|
63
|
+
todos.clear() // empty the list
|
|
64
|
+
user.name = "Ravi" // change one field
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Reading is safe even before data arrives: `user.name` is just empty while `user` is `null`.
|
|
68
|
+
|
|
69
|
+
### Remember things after the app closes
|
|
70
|
+
|
|
71
|
+
Put `saved` in front of `state`:
|
|
72
|
+
|
|
73
|
+
```orv
|
|
74
|
+
saved state cart = []
|
|
75
|
+
saved state darkMode = false
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The value is stored on the device (browser storage on the web, UserDefaults on
|
|
79
|
+
iPhone, SharedPreferences on Android) and comes back the next time the app opens.
|
|
80
|
+
It also survives live reload while you develop.
|
|
81
|
+
|
|
82
|
+
### Internet data
|
|
83
|
+
|
|
84
|
+
```orv
|
|
85
|
+
state users = []
|
|
86
|
+
|
|
87
|
+
start {
|
|
88
|
+
users = wait fetch("https://api.example.com/users")
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`fetch` gives back the JSON directly. To send data: `fetch(url, { method: "POST", body: { name: "A" } })`.
|
|
93
|
+
`wait` pauses until it's done: use it with `fetch` and `sleep(1000)`.
|
|
94
|
+
|
|
95
|
+
### Built-in functions
|
|
96
|
+
|
|
97
|
+
| Function | Does |
|
|
98
|
+
|---|---|
|
|
99
|
+
| `print(x)` | Log to the console |
|
|
100
|
+
| `fetch(url)` | Get data from the internet |
|
|
101
|
+
| `go(Screen)` | Open another screen |
|
|
102
|
+
| `sleep(ms)` | Wait (use with `wait`) |
|
|
103
|
+
| `random(1, 6)` | Random whole number |
|
|
104
|
+
| `len(list)` | Number of items |
|
|
105
|
+
|
|
106
|
+
Short functions: `todos.filter(t => t.done)`, `prices.map((p, i) => p * 2)`.
|
|
107
|
+
Choose a value: `done ? "Yes" : "No"`.
|
|
108
|
+
|
|
109
|
+
## Values
|
|
110
|
+
|
|
111
|
+
`12`, `3.5`, `"text"`, `"Hi {name}"` (interpolation), `true`, `false`, `null`,
|
|
112
|
+
`[1, 2, 3]`, `#1e293b` (colors), `a.b`, `list[0]`, `f(x)`.
|
|
113
|
+
|
|
114
|
+
Operators: `+ - * / %`, `== != < > <= >=`, `&& || !`.
|
|
115
|
+
|
|
116
|
+
## Elements
|
|
117
|
+
|
|
118
|
+
`Element firstArgument, name: value, name: value { children }`
|
|
119
|
+
|
|
120
|
+
| Element | First argument | Renders as |
|
|
121
|
+
|---|---|---|
|
|
122
|
+
| `Column` | — | vertical stack |
|
|
123
|
+
| `Row` | — | horizontal stack |
|
|
124
|
+
| `Box` | — | container |
|
|
125
|
+
| `Text`, `Heading` | text | text |
|
|
126
|
+
| `Button` | label | button |
|
|
127
|
+
| `Input` | placeholder | text field |
|
|
128
|
+
| `Image` | source URL | image |
|
|
129
|
+
| `Link` | text | link (`href: "..."`) |
|
|
130
|
+
|
|
131
|
+
Style props: `bg`, `color`, `size`, `padding`, `margin`, `gap`, `radius`, `width`,
|
|
132
|
+
`height`, `align`, `justify`, `border`, `shadow`, and the rest of Orvyn's style props.
|
|
133
|
+
Plain words like `black` or `center` are treated as text.
|
|
134
|
+
|
|
135
|
+
Events: `onTap`, `onChange`, `onSubmit`, `onHover`, `onLeave`. Give a function name,
|
|
136
|
+
one statement, or a block:
|
|
137
|
+
|
|
138
|
+
```orv
|
|
139
|
+
Button "Add", onTap: increment
|
|
140
|
+
Button "-1", onTap: count -= 1
|
|
141
|
+
Input "Name", onChange: { name = value } // value = the typed text
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Inside a view you can use `if` / `else` and `for item in list { ... }`.
|
|
145
|
+
|
|
146
|
+
### Screens and navigation
|
|
147
|
+
|
|
148
|
+
```orv
|
|
149
|
+
screen Home {
|
|
150
|
+
Button "About", onTap: go(About)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
screen About {
|
|
154
|
+
Button "Back", onTap: go(Home)
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`App` (or the first screen in the file) opens first.
|
|
159
|
+
|
|
160
|
+
## Editor colours (VS Code)
|
|
161
|
+
|
|
162
|
+
Install **Orvyn Language** from the VS Code Extensions panel.
|
|
163
|
+
|
|
164
|
+
## Errors
|
|
165
|
+
|
|
166
|
+
The compiler stops at the first error and shows the line:
|
|
167
|
+
|
|
168
|
+
```
|
|
169
|
+
✗ app.orv:4:3
|
|
170
|
+
Unknown element "Buton". Available: Column, Row, Box, Text, ...
|
|
171
|
+
|
|
172
|
+
4 | Buton "x"
|
|
173
|
+
| ^
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Not in v0.1 yet
|
|
177
|
+
|
|
178
|
+
- Native: `Image` sizes, `justify`, and hover events are basic on phones
|
|
179
|
+
- Live reload is iPhone-only for now (Android: `orvyn android` rebuilds)
|
|
180
|
+
- `state` inside components (keep state at the top of the file for now)
|
|
181
|
+
- Browser back button with `go()`
|
|
182
|
+
- `while` loops
|