Compare commits
9 Commits
843955f2b0
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| eba41e4330 | |||
| a894fc6689 | |||
| f91d5a5091 | |||
| d2e89dae3e | |||
| c2fea96feb | |||
| e59e340777 | |||
| 10438691c2 | |||
| a4fb422b83 | |||
| bae4d5aca6 |
@@ -0,0 +1 @@
|
||||
target
|
||||
@@ -0,0 +1,12 @@
|
||||
local two
|
||||
do
|
||||
local one
|
||||
if something then
|
||||
local three
|
||||
print(something)
|
||||
end
|
||||
local four
|
||||
if something2 then
|
||||
|
||||
end
|
||||
end
|
||||
|
Before Width: | Height: | Size: 254 KiB After Width: | Height: | Size: 254 KiB |
+478
@@ -0,0 +1,478 @@
|
||||
// Credit of most code to https://sotrh.github.io/learn-wgpu/ since I'm not familiar with wgpu
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
use winit::dpi::PhysicalPosition;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use winit::platform::web::EventLoopExtWebSys;
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::*,
|
||||
event_loop::{ActiveEventLoop, EventLoop},
|
||||
keyboard::{KeyCode, PhysicalKey},
|
||||
window::Window,
|
||||
};
|
||||
|
||||
// This will store the state of our game
|
||||
pub struct State {
|
||||
surface: wgpu::Surface<'static>,
|
||||
device: wgpu::Device,
|
||||
queue: wgpu::Queue,
|
||||
config: wgpu::SurfaceConfiguration,
|
||||
is_surface_configured: bool,
|
||||
window: Arc<Window>,
|
||||
render_pipeline1: wgpu::RenderPipeline,
|
||||
render_pipeline2: wgpu::RenderPipeline,
|
||||
other_pipeline: bool,
|
||||
sky: wgpu::Color,
|
||||
}
|
||||
|
||||
impl State {
|
||||
// We don't need this to be async right now,
|
||||
// but we will in the next tutorial
|
||||
pub async fn new(window: Arc<Window>) -> anyhow::Result<Self> {
|
||||
let size = window.inner_size();
|
||||
|
||||
// The instance is a handle to our GPU
|
||||
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
backends: wgpu::Backends::PRIMARY,
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
backends: wgpu::Backends::GL,
|
||||
flags: Default::default(),
|
||||
memory_budget_thresholds: Default::default(),
|
||||
backend_options: Default::default(),
|
||||
display: None,
|
||||
});
|
||||
|
||||
let surface = instance.create_surface(window.clone()).unwrap();
|
||||
|
||||
let adapter = instance
|
||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||
power_preference: wgpu::PowerPreference::default(),
|
||||
compatible_surface: Some(&surface),
|
||||
force_fallback_adapter: false,
|
||||
})
|
||||
.await?;
|
||||
|
||||
let (device, queue) = adapter
|
||||
.request_device(&wgpu::DeviceDescriptor {
|
||||
label: None,
|
||||
required_features: wgpu::Features::empty(),
|
||||
experimental_features: wgpu::ExperimentalFeatures::disabled(),
|
||||
// WebGL doesn't support all of wgpu's features, so if
|
||||
// we're building for the web we'll have to disable some.
|
||||
required_limits: if cfg!(target_arch = "wasm32") {
|
||||
wgpu::Limits::downlevel_webgl2_defaults()
|
||||
} else {
|
||||
wgpu::Limits::default()
|
||||
},
|
||||
memory_hints: Default::default(),
|
||||
trace: wgpu::Trace::Off,
|
||||
})
|
||||
.await?;
|
||||
|
||||
let surface_caps = surface.get_capabilities(&adapter);
|
||||
// Shader code in this tutorial assumes an sRGB surface texture. Using a different
|
||||
// one will result in all the colors coming out darker. If you want to support non
|
||||
// sRGB surfaces, you'll need to account for that when drawing to the frame.
|
||||
let surface_format = surface_caps
|
||||
.formats
|
||||
.iter()
|
||||
.find(|f| f.is_srgb())
|
||||
.copied()
|
||||
.unwrap_or(surface_caps.formats[0]);
|
||||
let config = wgpu::SurfaceConfiguration {
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||
format: surface_format,
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
present_mode: surface_caps.present_modes[0],
|
||||
alpha_mode: surface_caps.alpha_modes[0],
|
||||
view_formats: vec![],
|
||||
desired_maximum_frame_latency: 2,
|
||||
};
|
||||
|
||||
let shader1 = device.create_shader_module(wgpu::include_wgsl!("assets/shader.wgsl"));
|
||||
|
||||
let render_pipeline_layout1 =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: &[],
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let render_pipeline1 = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&render_pipeline_layout1),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader1,
|
||||
entry_point: Some("vs_main"), // 1.
|
||||
buffers: &[], // 2.
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
// 3.
|
||||
module: &shader1,
|
||||
entry_point: Some("fs_main"),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
// 4.
|
||||
format: config.format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList, // 1.
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw, // 2.
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
// Requires Features::DEPTH_CLIP_CONTROL
|
||||
unclipped_depth: false,
|
||||
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None, // 1.
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1, // 2.
|
||||
mask: !0, // 3.
|
||||
alpha_to_coverage_enabled: false, // 4.
|
||||
},
|
||||
multiview_mask: None, // 5.
|
||||
cache: None, // 6.
|
||||
});
|
||||
|
||||
let shader2 = device.create_shader_module(wgpu::include_wgsl!("assets/shader2.wgsl"));
|
||||
|
||||
let render_pipeline_layout2 =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: &[],
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let render_pipeline2 = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&render_pipeline_layout2),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader2,
|
||||
entry_point: Some("vs_main"), // 1.
|
||||
buffers: &[], // 2.
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
// 3.
|
||||
module: &shader2,
|
||||
entry_point: Some("fs_main"),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
// 4.
|
||||
format: config.format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList, // 1.
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw, // 2.
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
// Requires Features::DEPTH_CLIP_CONTROL
|
||||
unclipped_depth: false,
|
||||
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None, // 1.
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1, // 2.
|
||||
mask: !0, // 3.
|
||||
alpha_to_coverage_enabled: false, // 4.
|
||||
},
|
||||
multiview_mask: None, // 5.
|
||||
cache: None, // 6.
|
||||
});
|
||||
|
||||
Ok(Self {
|
||||
surface,
|
||||
device,
|
||||
queue,
|
||||
config,
|
||||
is_surface_configured: false,
|
||||
window,
|
||||
render_pipeline1,
|
||||
render_pipeline2,
|
||||
other_pipeline: false,
|
||||
sky: wgpu::Color {
|
||||
r: 0.1,
|
||||
g: 0.2,
|
||||
b: 0.3,
|
||||
a: 1.0,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, width: u32, height: u32) {
|
||||
if width > 0 && height > 0 {
|
||||
let max = 2048;
|
||||
self.config.width = width.min(max);
|
||||
self.config.height = height.min(max);
|
||||
self.surface.configure(&self.device, &self.config);
|
||||
self.is_surface_configured = true;
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
// ...
|
||||
}
|
||||
|
||||
fn render(&mut self) -> anyhow::Result<()> {
|
||||
self.window.request_redraw();
|
||||
|
||||
// We can't render unless the surface is configured
|
||||
if !self.is_surface_configured {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let output = match self.surface.get_current_texture() {
|
||||
wgpu::CurrentSurfaceTexture::Success(surface_texture) => surface_texture,
|
||||
wgpu::CurrentSurfaceTexture::Suboptimal(surface_texture) => {
|
||||
self.surface.configure(&self.device, &self.config);
|
||||
surface_texture
|
||||
}
|
||||
wgpu::CurrentSurfaceTexture::Timeout
|
||||
| wgpu::CurrentSurfaceTexture::Occluded
|
||||
| wgpu::CurrentSurfaceTexture::Validation => {
|
||||
// Skip this frame
|
||||
return Ok(());
|
||||
}
|
||||
wgpu::CurrentSurfaceTexture::Outdated => {
|
||||
self.surface.configure(&self.device, &self.config);
|
||||
return Ok(());
|
||||
}
|
||||
wgpu::CurrentSurfaceTexture::Lost => {
|
||||
// You could recreate the devices and all resources
|
||||
// created with it here, but we'll just bail
|
||||
anyhow::bail!("Lost device");
|
||||
}
|
||||
};
|
||||
let view = output
|
||||
.texture
|
||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let mut encoder = self
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("Render Encoder"),
|
||||
});
|
||||
{
|
||||
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Render Pass"),
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: &view,
|
||||
resolve_target: None,
|
||||
depth_slice: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(self.sky),
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
occlusion_query_set: None,
|
||||
timestamp_writes: None,
|
||||
multiview_mask: None,
|
||||
});
|
||||
|
||||
if self.other_pipeline {
|
||||
render_pass.set_pipeline(&self.render_pipeline1);
|
||||
} else {
|
||||
render_pass.set_pipeline(&self.render_pipeline2);
|
||||
}
|
||||
render_pass.draw(0..3, 0..1); // 3.
|
||||
}
|
||||
|
||||
// submit will accept anything that implements IntoIter
|
||||
self.queue.submit(std::iter::once(encoder.finish()));
|
||||
output.present();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_key(&mut self, event_loop: &ActiveEventLoop, code: KeyCode, is_pressed: bool) {
|
||||
match (code, is_pressed) {
|
||||
(KeyCode::Escape, true) => event_loop.exit(),
|
||||
(KeyCode::Space, true) => {
|
||||
self.other_pipeline = !self.other_pipeline;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_mouse_moved(&mut self, position: PhysicalPosition<f64>) {
|
||||
self.sky = wgpu::Color {
|
||||
r: 0.3,
|
||||
g: position.x / 1000.0,
|
||||
b: position.y / 1000.0,
|
||||
a: 1.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
proxy: Option<winit::event_loop::EventLoopProxy<State>>,
|
||||
state: Option<State>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(#[cfg(target_arch = "wasm32")] event_loop: &EventLoop<State>) -> Self {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let proxy = Some(event_loop.create_proxy());
|
||||
Self {
|
||||
state: None,
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
proxy,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler<State> for App {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
#[allow(unused_mut)]
|
||||
let mut window_attributes = Window::default_attributes();
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
use wasm_bindgen::JsCast;
|
||||
use winit::platform::web::WindowAttributesExtWebSys;
|
||||
|
||||
const CANVAS_ID: &str = "canvas";
|
||||
|
||||
let window = wgpu::web_sys::window().unwrap_throw();
|
||||
let document = window.document().unwrap_throw();
|
||||
let canvas = document.get_element_by_id(CANVAS_ID).unwrap_throw();
|
||||
let html_canvas_element = canvas.unchecked_into();
|
||||
window_attributes = window_attributes.with_canvas(Some(html_canvas_element));
|
||||
}
|
||||
|
||||
let window = Arc::new(event_loop.create_window(window_attributes).unwrap());
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
// If we are not on web we can use pollster to
|
||||
// await the window creation
|
||||
self.state = Some(pollster::block_on(State::new(window)).unwrap());
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
// Run the future asynchronously and use the
|
||||
// proxy to send the results to the event loop
|
||||
if let Some(proxy) = self.proxy.take() {
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
assert!(
|
||||
proxy
|
||||
.send_event(
|
||||
State::new(window)
|
||||
.await
|
||||
.expect("Unable to create canvas!!!")
|
||||
)
|
||||
.is_ok()
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_mut)]
|
||||
fn user_event(&mut self, _event_loop: &ActiveEventLoop, mut event: State) {
|
||||
// This is where proxy.send_event() ends up
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
event.window.request_redraw();
|
||||
event.resize(
|
||||
event.window.inner_size().width,
|
||||
event.window.inner_size().height,
|
||||
);
|
||||
}
|
||||
self.state = Some(event);
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop: &ActiveEventLoop,
|
||||
_window_id: winit::window::WindowId,
|
||||
event: WindowEvent,
|
||||
) {
|
||||
let state = match &mut self.state {
|
||||
Some(canvas) => canvas,
|
||||
None => return,
|
||||
};
|
||||
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::Resized(size) => state.resize(size.width, size.height),
|
||||
WindowEvent::RedrawRequested => {
|
||||
state.update();
|
||||
match state.render() {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
// Log the error and exit gracefully
|
||||
log::error!("{e}");
|
||||
event_loop.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
WindowEvent::CursorMoved { position: pos, .. } => state.handle_mouse_moved(pos),
|
||||
WindowEvent::KeyboardInput {
|
||||
event:
|
||||
KeyEvent {
|
||||
physical_key: PhysicalKey::Code(code),
|
||||
state: key_state,
|
||||
..
|
||||
},
|
||||
..
|
||||
} => state.handle_key(event_loop, code, key_state.is_pressed()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run() -> anyhow::Result<()> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
env_logger::init();
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
console_log::init_with_level(log::Level::Info).unwrap_throw();
|
||||
}
|
||||
|
||||
let event_loop = EventLoop::with_user_event().build()?;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let mut app = App::new();
|
||||
event_loop.run_app(&mut app)?;
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let app = App::new(&event_loop);
|
||||
event_loop.spawn_app(app);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn run_web() -> Result<(), wasm_bindgen::JsValue> {
|
||||
console_error_panic_hook::set_once();
|
||||
run().unwrap_throw();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
print("hello world")
|
||||
@@ -1,11 +0,0 @@
|
||||
/*
|
||||
@native @inline
|
||||
@expr(say (&a: String) (b: i32) times)
|
||||
fn say_something(&a: String, b: i32) -> Pack<String,i32> {
|
||||
for index in 0..b {
|
||||
a += a
|
||||
}
|
||||
return {a,b}
|
||||
}
|
||||
*/
|
||||
println("where the huzz")
|
||||
+1212
-334
File diff suppressed because it is too large
Load Diff
+2
-481
@@ -1,482 +1,3 @@
|
||||
#![recursion_limit = "256"]
|
||||
mod lang;
|
||||
mod neural;
|
||||
|
||||
// Credit of most code to https://sotrh.github.io/learn-wgpu/ since I come from OpenGl not wgpu
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
use winit::dpi::PhysicalPosition;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use winit::platform::web::EventLoopExtWebSys;
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::*,
|
||||
event_loop::{ActiveEventLoop, EventLoop},
|
||||
keyboard::{KeyCode, PhysicalKey},
|
||||
window::Window,
|
||||
};
|
||||
|
||||
// This will store the state of our game
|
||||
pub struct State {
|
||||
surface: wgpu::Surface<'static>,
|
||||
device: wgpu::Device,
|
||||
queue: wgpu::Queue,
|
||||
config: wgpu::SurfaceConfiguration,
|
||||
is_surface_configured: bool,
|
||||
window: Arc<Window>,
|
||||
render_pipeline1: wgpu::RenderPipeline,
|
||||
render_pipeline2: wgpu::RenderPipeline,
|
||||
other_pipeline: bool,
|
||||
sky: wgpu::Color,
|
||||
}
|
||||
|
||||
impl State {
|
||||
// We don't need this to be async right now,
|
||||
// but we will in the next tutorial
|
||||
pub async fn new(window: Arc<Window>) -> anyhow::Result<Self> {
|
||||
let size = window.inner_size();
|
||||
|
||||
// The instance is a handle to our GPU
|
||||
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
backends: wgpu::Backends::PRIMARY,
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
backends: wgpu::Backends::GL,
|
||||
flags: Default::default(),
|
||||
memory_budget_thresholds: Default::default(),
|
||||
backend_options: Default::default(),
|
||||
display: None,
|
||||
});
|
||||
|
||||
let surface = instance.create_surface(window.clone()).unwrap();
|
||||
|
||||
let adapter = instance
|
||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||
power_preference: wgpu::PowerPreference::default(),
|
||||
compatible_surface: Some(&surface),
|
||||
force_fallback_adapter: false,
|
||||
})
|
||||
.await?;
|
||||
|
||||
let (device, queue) = adapter
|
||||
.request_device(&wgpu::DeviceDescriptor {
|
||||
label: None,
|
||||
required_features: wgpu::Features::empty(),
|
||||
experimental_features: wgpu::ExperimentalFeatures::disabled(),
|
||||
// WebGL doesn't support all of wgpu's features, so if
|
||||
// we're building for the web we'll have to disable some.
|
||||
required_limits: if cfg!(target_arch = "wasm32") {
|
||||
wgpu::Limits::downlevel_webgl2_defaults()
|
||||
} else {
|
||||
wgpu::Limits::default()
|
||||
},
|
||||
memory_hints: Default::default(),
|
||||
trace: wgpu::Trace::Off,
|
||||
})
|
||||
.await?;
|
||||
|
||||
let surface_caps = surface.get_capabilities(&adapter);
|
||||
// Shader code in this tutorial assumes an sRGB surface texture. Using a different
|
||||
// one will result in all the colors coming out darker. If you want to support non
|
||||
// sRGB surfaces, you'll need to account for that when drawing to the frame.
|
||||
let surface_format = surface_caps
|
||||
.formats
|
||||
.iter()
|
||||
.find(|f| f.is_srgb())
|
||||
.copied()
|
||||
.unwrap_or(surface_caps.formats[0]);
|
||||
let config = wgpu::SurfaceConfiguration {
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||
format: surface_format,
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
present_mode: surface_caps.present_modes[0],
|
||||
alpha_mode: surface_caps.alpha_modes[0],
|
||||
view_formats: vec![],
|
||||
desired_maximum_frame_latency: 2,
|
||||
};
|
||||
|
||||
let shader1 = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl"));
|
||||
|
||||
let render_pipeline_layout1 =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: &[],
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let render_pipeline1 = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&render_pipeline_layout1),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader1,
|
||||
entry_point: Some("vs_main"), // 1.
|
||||
buffers: &[], // 2.
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
// 3.
|
||||
module: &shader1,
|
||||
entry_point: Some("fs_main"),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
// 4.
|
||||
format: config.format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList, // 1.
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw, // 2.
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
// Requires Features::DEPTH_CLIP_CONTROL
|
||||
unclipped_depth: false,
|
||||
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None, // 1.
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1, // 2.
|
||||
mask: !0, // 3.
|
||||
alpha_to_coverage_enabled: false, // 4.
|
||||
},
|
||||
multiview_mask: None, // 5.
|
||||
cache: None, // 6.
|
||||
});
|
||||
|
||||
let shader2 = device.create_shader_module(wgpu::include_wgsl!("shader2.wgsl"));
|
||||
|
||||
let render_pipeline_layout2 =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: &[],
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let render_pipeline2 = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&render_pipeline_layout2),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader2,
|
||||
entry_point: Some("vs_main"), // 1.
|
||||
buffers: &[], // 2.
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
// 3.
|
||||
module: &shader2,
|
||||
entry_point: Some("fs_main"),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
// 4.
|
||||
format: config.format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList, // 1.
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw, // 2.
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
// Requires Features::DEPTH_CLIP_CONTROL
|
||||
unclipped_depth: false,
|
||||
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None, // 1.
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1, // 2.
|
||||
mask: !0, // 3.
|
||||
alpha_to_coverage_enabled: false, // 4.
|
||||
},
|
||||
multiview_mask: None, // 5.
|
||||
cache: None, // 6.
|
||||
});
|
||||
|
||||
Ok(Self {
|
||||
surface,
|
||||
device,
|
||||
queue,
|
||||
config,
|
||||
is_surface_configured: false,
|
||||
window,
|
||||
render_pipeline1,
|
||||
render_pipeline2,
|
||||
other_pipeline: false,
|
||||
sky: wgpu::Color {
|
||||
r: 0.1,
|
||||
g: 0.2,
|
||||
b: 0.3,
|
||||
a: 1.0,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, width: u32, height: u32) {
|
||||
if width > 0 && height > 0 {
|
||||
let max = 2048;
|
||||
self.config.width = width.min(max);
|
||||
self.config.height = height.min(max);
|
||||
self.surface.configure(&self.device, &self.config);
|
||||
self.is_surface_configured = true;
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
// ...
|
||||
}
|
||||
|
||||
fn render(&mut self) -> anyhow::Result<()> {
|
||||
self.window.request_redraw();
|
||||
|
||||
// We can't render unless the surface is configured
|
||||
if !self.is_surface_configured {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let output = match self.surface.get_current_texture() {
|
||||
wgpu::CurrentSurfaceTexture::Success(surface_texture) => surface_texture,
|
||||
wgpu::CurrentSurfaceTexture::Suboptimal(surface_texture) => {
|
||||
self.surface.configure(&self.device, &self.config);
|
||||
surface_texture
|
||||
}
|
||||
wgpu::CurrentSurfaceTexture::Timeout
|
||||
| wgpu::CurrentSurfaceTexture::Occluded
|
||||
| wgpu::CurrentSurfaceTexture::Validation => {
|
||||
// Skip this frame
|
||||
return Ok(());
|
||||
}
|
||||
wgpu::CurrentSurfaceTexture::Outdated => {
|
||||
self.surface.configure(&self.device, &self.config);
|
||||
return Ok(());
|
||||
}
|
||||
wgpu::CurrentSurfaceTexture::Lost => {
|
||||
// You could recreate the devices and all resources
|
||||
// created with it here, but we'll just bail
|
||||
anyhow::bail!("Lost device");
|
||||
}
|
||||
};
|
||||
let view = output
|
||||
.texture
|
||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let mut encoder = self
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("Render Encoder"),
|
||||
});
|
||||
{
|
||||
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Render Pass"),
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: &view,
|
||||
resolve_target: None,
|
||||
depth_slice: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(self.sky),
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
occlusion_query_set: None,
|
||||
timestamp_writes: None,
|
||||
multiview_mask: None,
|
||||
});
|
||||
|
||||
if self.other_pipeline {
|
||||
render_pass.set_pipeline(&self.render_pipeline1);
|
||||
} else {
|
||||
render_pass.set_pipeline(&self.render_pipeline2);
|
||||
}
|
||||
render_pass.draw(0..3, 0..1); // 3.
|
||||
}
|
||||
|
||||
// submit will accept anything that implements IntoIter
|
||||
self.queue.submit(std::iter::once(encoder.finish()));
|
||||
output.present();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_key(&mut self, event_loop: &ActiveEventLoop, code: KeyCode, is_pressed: bool) {
|
||||
match (code, is_pressed) {
|
||||
(KeyCode::Escape, true) => event_loop.exit(),
|
||||
(KeyCode::Space, true) => {
|
||||
self.other_pipeline = !self.other_pipeline;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_mouse_moved(&mut self, position: PhysicalPosition<f64>) {
|
||||
self.sky = wgpu::Color {
|
||||
r: 0.3,
|
||||
g: position.x / 1000.0,
|
||||
b: position.y / 1000.0,
|
||||
a: 1.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
proxy: Option<winit::event_loop::EventLoopProxy<State>>,
|
||||
state: Option<State>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(#[cfg(target_arch = "wasm32")] event_loop: &EventLoop<State>) -> Self {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let proxy = Some(event_loop.create_proxy());
|
||||
Self {
|
||||
state: None,
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
proxy,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler<State> for App {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
#[allow(unused_mut)]
|
||||
let mut window_attributes = Window::default_attributes();
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
use wasm_bindgen::JsCast;
|
||||
use winit::platform::web::WindowAttributesExtWebSys;
|
||||
|
||||
const CANVAS_ID: &str = "canvas";
|
||||
|
||||
let window = wgpu::web_sys::window().unwrap_throw();
|
||||
let document = window.document().unwrap_throw();
|
||||
let canvas = document.get_element_by_id(CANVAS_ID).unwrap_throw();
|
||||
let html_canvas_element = canvas.unchecked_into();
|
||||
window_attributes = window_attributes.with_canvas(Some(html_canvas_element));
|
||||
}
|
||||
|
||||
let window = Arc::new(event_loop.create_window(window_attributes).unwrap());
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
// If we are not on web we can use pollster to
|
||||
// await the window creation
|
||||
self.state = Some(pollster::block_on(State::new(window)).unwrap());
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
// Run the future asynchronously and use the
|
||||
// proxy to send the results to the event loop
|
||||
if let Some(proxy) = self.proxy.take() {
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
assert!(
|
||||
proxy
|
||||
.send_event(
|
||||
State::new(window)
|
||||
.await
|
||||
.expect("Unable to create canvas!!!")
|
||||
)
|
||||
.is_ok()
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_mut)]
|
||||
fn user_event(&mut self, _event_loop: &ActiveEventLoop, mut event: State) {
|
||||
// This is where proxy.send_event() ends up
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
event.window.request_redraw();
|
||||
event.resize(
|
||||
event.window.inner_size().width,
|
||||
event.window.inner_size().height,
|
||||
);
|
||||
}
|
||||
self.state = Some(event);
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop: &ActiveEventLoop,
|
||||
_window_id: winit::window::WindowId,
|
||||
event: WindowEvent,
|
||||
) {
|
||||
let state = match &mut self.state {
|
||||
Some(canvas) => canvas,
|
||||
None => return,
|
||||
};
|
||||
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::Resized(size) => state.resize(size.width, size.height),
|
||||
WindowEvent::RedrawRequested => {
|
||||
state.update();
|
||||
match state.render() {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
// Log the error and exit gracefully
|
||||
log::error!("{e}");
|
||||
event_loop.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
WindowEvent::CursorMoved { position: pos, .. } => state.handle_mouse_moved(pos),
|
||||
WindowEvent::KeyboardInput {
|
||||
event:
|
||||
KeyEvent {
|
||||
physical_key: PhysicalKey::Code(code),
|
||||
state: key_state,
|
||||
..
|
||||
},
|
||||
..
|
||||
} => state.handle_key(event_loop, code, key_state.is_pressed()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run() -> anyhow::Result<()> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
env_logger::init();
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
console_log::init_with_level(log::Level::Info).unwrap_throw();
|
||||
}
|
||||
|
||||
let event_loop = EventLoop::with_user_event().build()?;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let mut app = App::new();
|
||||
event_loop.run_app(&mut app)?;
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let app = App::new(&event_loop);
|
||||
event_loop.spawn_app(app);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn run_web() -> Result<(), wasm_bindgen::JsValue> {
|
||||
console_error_panic_hook::set_once();
|
||||
run().unwrap_throw();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
pub mod engine;
|
||||
pub mod lang;
|
||||
+3
-2
@@ -1,7 +1,8 @@
|
||||
#![recursion_limit = "256"]
|
||||
|
||||
use game::run;
|
||||
use game::*;
|
||||
|
||||
fn main() {
|
||||
run().unwrap();
|
||||
|
||||
//engine::run().unwrap();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,353 @@
|
||||
// Macro for expanding syntax form into a struct
|
||||
macro_rules! definition {
|
||||
{@build ($n:ident structure) ($($t:tt)*) null () } => {
|
||||
#[allow(unused)]
|
||||
#[derive(Debug)]
|
||||
struct $n ($($t)*);
|
||||
};
|
||||
{@build ($n:ident enumeration new ($($f:tt)*) ($m:ident($($mt:tt)*)$($r:tt)*)) () null ()} => {
|
||||
definition!{@build ($n enumeration ($($f)*) ($m($($mt)*)$($r)*)) () null () $($mt)*}
|
||||
};
|
||||
{@build ($n:ident enumeration ($($f:tt)*) ($m:ident($($mt:tt)*)$($r:tt)*)) ($($t:tt)*) null ()} => {
|
||||
definition!{@build ($n enumeration new ($($f)*$m($($t)*)) ($($r)*)) () null () }
|
||||
};
|
||||
{@build ($n:ident enumeration new ($($m:ident($($mt:tt)*))*) ()) () null () } => {
|
||||
definition!{@build ($n enumeration filter () ($($m($($mt)*))*))}
|
||||
};
|
||||
{@build ($n:ident enumeration filter ($($e:tt)*) ($m:ident($($mt:tt)+)$($t:tt)*))} => {
|
||||
definition!{@build ($n enumeration filter ($($e)*$m($($mt)*),) ($($t)*))}
|
||||
};
|
||||
{@build ($n:ident enumeration filter ($($e:tt)*) ($m:ident()$($t:tt)*))} => {
|
||||
definition!{@build ($n enumeration filter ($($e)*$m,) ($($t)*))}
|
||||
};
|
||||
{@build ($n:ident enumeration filter ($($e:tt)*) ())} => {
|
||||
#[allow(unused)]
|
||||
#[derive(Debug)]
|
||||
enum $n {
|
||||
$($e)*
|
||||
}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) null () $l:literal $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*) null () $($r)*}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) null () $i:ident $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*Box<$i>,) null () $($r)*}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) option ($a:tt) () $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*Option<$a>,) null () $($r)*}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) vector ($a:tt) () $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*Vec<$a>,) null () $($r)*}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) option ($($a:tt)*) () $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*Option<($($a),*)>,) null () $($r)*}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) vector ($($a:tt)*) () $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*Vec<($($a),*)>,) null () $($r)*}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) null () [$($b:tt)*] $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*) option () ($($b)*) $($r)*}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) null () {$($b:tt)*} $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*) vector () ($($b)*) $($r)*}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) $i:ident ($($a:tt)*) ($l:literal $($b:tt)*) $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*) $i ($($a)*) ($($b)*) $($r)*}
|
||||
};
|
||||
{@build $m:tt ($($t:tt)*) $i:ident ($($a:tt)*) ($n:ident $($b:tt)*) $($r:tt)*} => {
|
||||
definition!{@build $m ($($t)*) $i ($($a)*$n) ($($b)*) $($r)*}
|
||||
};
|
||||
}
|
||||
macro_rules! implement {
|
||||
{@begin $n:ident structure $($t:tt)*} => {
|
||||
implement!{@build (state result) (structure $n) {} {} $($t)*}
|
||||
};
|
||||
{@begin $n:ident enumeration ($m:ident ($($t2:tt)*)$($t:tt)*)} => {
|
||||
implement!{@build (state result) (enumeration $m {} ($($t)*) $n) {} {} $($t2)*}
|
||||
};
|
||||
{@build ($st:ident $r:ident) $m:tt $f:tt {$($f2:tt)*} $s:literal $($t:tt)*} => {
|
||||
implement!{@build ($st $r) $m $f {
|
||||
$($f2)*
|
||||
$st = $st.trim_start();
|
||||
if $st.starts_with($s) {
|
||||
$st = &$st[$s.len()..]
|
||||
} else {
|
||||
return Err("error".to_string())
|
||||
}
|
||||
} $($t)*}
|
||||
};
|
||||
{@build ($s:ident $r:ident) $m:tt {$($f:tt)*} {$($f2:tt)*} $i:ident $($t:tt)*} => {
|
||||
implement!{@build ($s $r) $m {$($f)*{$($f2)*}} {
|
||||
let $r;
|
||||
match $i::consume($s) {
|
||||
Ok((r,s)) => {
|
||||
$s = s;
|
||||
$r = r;
|
||||
}
|
||||
Err(error) => {
|
||||
return Err(error);
|
||||
}
|
||||
}
|
||||
} $($t)*}
|
||||
};
|
||||
{@build $va:tt ($($m:tt)*) {$($f:tt)*} {$($f2:tt)*} {$($r:tt)*} $($t:tt)*} => {
|
||||
implement!{@build $va (vector {$($f)*{$($f2)*}} ($($t)*) $($m)*) {} {} $($r)*}
|
||||
};
|
||||
{@build $va:tt ($($m:tt)*) {$($f:tt)*} {$($f2:tt)*} [$($r:tt)*] $($t:tt)*} => {
|
||||
implement!{@build $va (option {$($f)*{$($f2)*}} ($($t)*) $($m)*) {} {} $($r)*}
|
||||
};
|
||||
{@finish ($s:ident $re:ident) (option {$($l:tt)*} ($($t:tt)*) $($m:tt)*) {{$($f:tt)*}$({$($r:tt)*})*} } => {
|
||||
implement!{@build ($s $re) ($($m)*) {$($l)*} {
|
||||
let mut $re;
|
||||
let taker = move |_state| {
|
||||
let mut $s: &str = _state;
|
||||
$($f)*
|
||||
Ok(((
|
||||
$({$($r)*$re}),*
|
||||
),$s))
|
||||
};
|
||||
match taker($s) {
|
||||
Ok((r,s)) => {
|
||||
$s = s;
|
||||
$re = Some(r);
|
||||
}
|
||||
Err(_) => {
|
||||
$re = None;
|
||||
}
|
||||
}
|
||||
} $($t)*}
|
||||
};
|
||||
{@finish ($s:ident $re:ident) (vector {$($l:tt)*} ($($t:tt)*) $($m:tt)*) {{$($f:tt)*}$({$($r:tt)*})*} } => {
|
||||
implement!{@build ($s $re) ($($m)*) {$($l)*} {
|
||||
let mut $re = Vec::new();
|
||||
let taker = move |_state| {
|
||||
let mut $s: &str = _state;
|
||||
$($f)*
|
||||
Ok(((
|
||||
$({$($r)*$re}),*
|
||||
),$s))
|
||||
};
|
||||
while !$s.is_empty() {
|
||||
match taker($s) {
|
||||
Ok((r,s)) => {
|
||||
$s = s;
|
||||
$re.push(r);
|
||||
}
|
||||
Err(error) => {
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
} $($t)*}
|
||||
};
|
||||
{@build $va:tt $m:tt {$($r:tt)*} $r2:tt} => {
|
||||
implement!{@finish $va $m {$($r)*$r2}}
|
||||
};
|
||||
{@finish ($s:ident $re:ident) (structure $n:ident) {{$($f:tt)*}$({$($r:tt)*})*} } => {
|
||||
#[allow(unused)]
|
||||
impl Parse for $n {
|
||||
fn consume(_state: &str) -> Result<(Self, &str), Error> where Self: Sized {
|
||||
let mut $s: &str = _state;
|
||||
$($f)*
|
||||
Ok((
|
||||
$n($({$($r)*$re.into()}),*),$s
|
||||
))
|
||||
}
|
||||
}
|
||||
};
|
||||
{@finish ($s:ident $re:ident) (enumeration $m2:ident {$($l:tt)*} ($m:ident ($($t2:tt)*)$($t:tt)*) $n:ident) {$f:tt$({$($r:tt)*})+} } => {
|
||||
implement!{@build ($s $re) (enumeration $m {$($l)*{
|
||||
$f
|
||||
Ok((
|
||||
$n::$m2($({$($r)*$re.into()}),+),$s
|
||||
))
|
||||
}} ($($t)*) $n) {} {} $($t2)*}
|
||||
};
|
||||
{@finish ($s:ident $re:ident) (enumeration $m3:ident {$($l:tt)*} ($m:ident ($($t2:tt)*)$($t:tt)*) $n:ident) {$f:tt} } => {
|
||||
implement!{@build ($s $re) (enumeration $m {$($l)*{
|
||||
$f
|
||||
Ok((
|
||||
$n::$m3,$s
|
||||
))
|
||||
}} ($($t)*) $n) {} {} $($t2)*}
|
||||
};
|
||||
{@finish ($s:ident $re:ident) (enumeration $m2:ident {$($l:tt)*} () $n:ident) {$f:tt$({$($r:tt)*})+}} => {
|
||||
implement!{@finish ($s $re) (enumeration {$($l)*{
|
||||
$f
|
||||
Ok((
|
||||
$n::$m2($({$($r)*$re.into()}),+),$s
|
||||
))
|
||||
}} () $n)} // straight to below
|
||||
};
|
||||
{@finish ($s:ident $re:ident) (enumeration $m2:ident {$($l:tt)*} () $n:ident) {$f:tt}} => {
|
||||
implement!{@finish ($s $re) (enumeration {$($l)*{
|
||||
$f
|
||||
Ok((
|
||||
$n::$m2,$s
|
||||
))
|
||||
}} () $n)} // straight to below
|
||||
};
|
||||
{@finish ($s:ident $re:ident) (enumeration {$({$($f:tt)*})*} () $n:ident)} => {
|
||||
#[allow(unused)]
|
||||
impl Parse for $n {
|
||||
fn consume(_state: &str) -> Result<(Self, &str), Error> where Self: Sized {
|
||||
let mut $s: &str = _state;
|
||||
$(
|
||||
let case = (||{
|
||||
let mut state: &str = _state;
|
||||
$($f)*
|
||||
})(); // put it into a closure and run it (makes returning easier)
|
||||
if case.is_ok() {
|
||||
return case;
|
||||
}
|
||||
)*
|
||||
Err("error".into())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! class {
|
||||
($i:ident($($t:tt)*)) => {
|
||||
definition!{@build ($i structure) () null () $($t)*}
|
||||
implement!{@begin $i structure $($t)*}
|
||||
//display!{@begin $i structure $($t)*}
|
||||
};
|
||||
($i:ident{$($n:ident($($t:tt)*)),*}) => {
|
||||
definition!{@build ($i enumeration new () ($($n($($t)*))*)) () null ()}
|
||||
implement!{@begin $i enumeration ($($n($($t)*))*)}
|
||||
//display!{@begin $i enumeration ($($n($($t)*))*)}
|
||||
}
|
||||
}
|
||||
macro_rules! parser {
|
||||
() => {};
|
||||
($i:ident$t:tt;$($tail:tt)*) => {
|
||||
class!($i$t);
|
||||
parser!($($tail)*);
|
||||
};
|
||||
}
|
||||
|
||||
// todo: reimplement attrib here
|
||||
// https://www.lua.org/manual/5.5/manual.html#9
|
||||
parser!{
|
||||
// LiteralString -> "..."
|
||||
// LiteralNumber -> 0x... 123.456
|
||||
Chunk ( Block );
|
||||
Block ( {Statement} [Return] );
|
||||
Statement {
|
||||
Semicolon (";"),
|
||||
Assignment (Variables "=" Expressions),
|
||||
Call (RootAtom CallChain),
|
||||
Label (Label),
|
||||
Break ("break"),
|
||||
Goto ("goto" Name),
|
||||
Do ("do" Block "end"),
|
||||
While ("while" Expression "do" Block "end"),
|
||||
Repeat ("repeat" Block "until" Expression),
|
||||
If ("if" Expression "then" Block {"elseif" Expression "then" Block} ["else" Block] "end"),
|
||||
ForRange ("for" Name "=" Expression "," Expression ["," Expression] "do" Block "end"),
|
||||
ForIn ("for" Names "in" Expressions "do" Block "end"),
|
||||
Function ("function" FunctionName FunctionBody),
|
||||
LocalFunction ("local" "function" Name FunctionBody),
|
||||
GlobalFunction ("global" "function" Name FunctionBody),
|
||||
Declaration ("local" Names ["=" Expressions]),
|
||||
Global ("global" Names)
|
||||
};
|
||||
Return( "return" [Expressions] [";"] );
|
||||
Label( "::" Name "::" );
|
||||
FunctionName( Name {"." Name} [":" Name] );
|
||||
Variables( Variable {"," Variable} );
|
||||
Names( Name {"," Name} );
|
||||
Expressions( Expression {"," Expression} );
|
||||
// DEVIATION: This parser is greedy, so the manual's syntax breaks it.
|
||||
RootAtom {
|
||||
Variable (Name),
|
||||
Expression ("(" Expression ")")
|
||||
};
|
||||
IndexAtom {
|
||||
Dot ("." Name),
|
||||
Index ("[" Expression "]")
|
||||
};
|
||||
CallAtom {
|
||||
Normal (Arguments),
|
||||
Method (":" Name Arguments)
|
||||
};
|
||||
PrefixAtom {
|
||||
Call (CallAtom),
|
||||
Index (IndexAtom)
|
||||
};
|
||||
VariableChain {
|
||||
Link (PrefixAtom VariableChain),
|
||||
Finish (IndexAtom)
|
||||
};
|
||||
CallChain {
|
||||
Link (PrefixAtom CallChain),
|
||||
Finish (CallAtom)
|
||||
};
|
||||
Variable (RootAtom VariableChain);
|
||||
Expression {
|
||||
Nil ("nil"),
|
||||
False ("false"),
|
||||
True ("true"),
|
||||
VarArg ("..."),
|
||||
Number (Number),
|
||||
String (String),
|
||||
FunctionDef (FunctionDef),
|
||||
Prefix (RootAtom {PrefixAtom}),
|
||||
Table (Table),
|
||||
Binary (Expression BinaryOp Expression), // todo: .
|
||||
Unary (UnaryOp Expression)
|
||||
};
|
||||
// -----------------------
|
||||
Arguments {
|
||||
Expressions ("(" [Expressions] ")"),
|
||||
Table (Table),
|
||||
String (String)
|
||||
};
|
||||
FunctionDef( "function" FunctionBody );
|
||||
FunctionBody( "(" [Parameters] ")" Block "end" );
|
||||
Parameters {
|
||||
Names (Names ["," VarArg]),
|
||||
VarArg (VarArg)
|
||||
};
|
||||
VarArg( "..." [Name] );
|
||||
Table( "{" [Fields] "}" );
|
||||
Fields( Field {FieldSep Field} [FieldSep] );
|
||||
Field {
|
||||
ExpressionIndex ("[" Expression "]" "=" Expression),
|
||||
Name (Name "=" Expression),
|
||||
Expression (Expression)
|
||||
};
|
||||
FieldSep {
|
||||
Comma (","),
|
||||
Semicolon (";")
|
||||
};
|
||||
BinaryOp {
|
||||
Plus ("+"),
|
||||
Minus ("-"),
|
||||
Mul ("*"),
|
||||
Divide ("/"),
|
||||
DivFloor ("//"),
|
||||
Caret ("^"),
|
||||
Modulo ("%"),
|
||||
Ampersand ("&"),
|
||||
Tilde ("~"),
|
||||
Pipe ("|"),
|
||||
ShiftRight (">>"),
|
||||
ShiftLeft ("<<"),
|
||||
Concat (".."),
|
||||
Less ("<"),
|
||||
LessEqual ("<="),
|
||||
Greater (">"),
|
||||
GreatEqual (">="),
|
||||
Equivalent ("=="),
|
||||
NotEqual ("~="),
|
||||
And ("and"),
|
||||
Or ("or")
|
||||
};
|
||||
UnaryOp {
|
||||
Negate ("-"),
|
||||
Not ("not"),
|
||||
Ampersand ("#"),
|
||||
Tilde ("~")
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user