Skip to content

Instantly share code, notes, and snippets.

View zesterer's full-sized avatar

Joshua Barretto zesterer

View GitHub Profile
@zesterer
zesterer / python-lex.rs
Last active July 15, 2021 23:09
Lexer for pythonic indentation-aware language that converts identation to token blocks *and* correctly handles parentheses
#[derive(Debug)]
enum Token {
Num(i64),
Paren(Vec<Token>),
Block(Vec<Token>),
}
fn lex(src: &str) -> Result<Vec<Token>, String> {
let mut ident_depth = 0;
let mut token_stack = vec![(ident_depth, Vec::new())];
fn ptr_at(pos: Vec2<isize>) -> *mut Px { ... }
type Px = u16;
pub unsafe fn line(&mut self, a: Vec2<isize>, b: Vec2<isize>, px: Px) {
let delta = b - a;
let delta_abs = delta.map(|e| e.abs() as usize);
let ptr = ptr_at(a.map(|e| e as usize));
let dx = if delta.x > 0 { 1 } else { -1 };
let dy = if delta.y > 0 { 1 } else { -1 } * WIDTH;
@zesterer
zesterer / block_on.rs
Created January 17, 2021 01:43
Unsafe no-alloc block_on
use super::*;
/// Synchronously block until a future completes.
///
/// # Safety
///
/// No waker provided to the future should ever be polled after the future returns `Poll::Ready`. The waker references
/// data on the blocking thread's stack and, as such, access to it after this point is UB because the data is no longer
/// a valid pointer.
#[allow(unused_unsafe)] // It's a stupid warning anyway. No such thing as unsafe code that's too explicit.
fn main() {
let s = r#"fn main() {
let s = @;
let x = s.find('@').unwrap();
println!("{}r#\"{}\"{}{}", &s[..x], s, '#', &s[x + 1..]);
}"#;
let x = s.find('@').unwrap();
println!("{}r#\"{}\"{}{}", &s[..x], s, '#', &s[x + 1..]);
}
use gui::{
widget::{Toggle, Button, Label, List},
layout::Direction,
event::Click,
Window, Widget,
};
fn main() {
enum Op {
Add,
diff --git a/assets/voxygen/shaders/figure-frag.glsl b/assets/voxygen/shaders/figure-frag.glsl
index 315807549..d5bea01a3 100644
--- a/assets/voxygen/shaders/figure-frag.glsl
+++ b/assets/voxygen/shaders/figure-frag.glsl
@@ -146,7 +146,7 @@ void main() {
// DirectionalLight sun_info = get_sun_info(sun_dir, sun_shade_frac, light_pos);
float point_shadow = shadow_at(f_pos, f_norm);
- DirectionalLight sun_info = get_sun_info(sun_dir, point_shadow * sun_shade_frac, /*sun_pos*/f_pos);
+ DirectionalLight sun_info = get_sun_info(sun_dir, point_shadow * sun_shade_frac, /*sun_pos*/f_pos, view_dir);
#version 330 core
#include <globals.glsl>
#include <srgb.glsl>
#include <random.glsl>
in vec3 v_pos;
in uint v_col;
in uint v_norm_ao;
in vec3 inst_pos;
# General utility
type Str = [Char]
type Io A = Universe -> (A, Universe)
fn nothing |uni of Universe| ((), uni)
fn print |s, uni| ((), @print(s, uni))
fn input |uni| @input(uni)
data Maybe A =
@zesterer
zesterer / infer.rs
Created May 14, 2020 21:28
Type Inference in 66 lines of Rust
use std::collections::HashMap;
#[derive(Debug)]
enum Type {
Num,
Bool,
List(Box<Type>),
Func(Box<Type>, Box<Type>),
}
diff --git a/src/lib.rs b/src/lib.rs
index 24534a8..8121dd9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -170,6 +170,11 @@ fn wait_lock<'a, T>(lock: &'a InnerMutex<T>) -> MutexGuard<'a, T> {
}
}
+#[inline]
+fn poll_lock<'a, T>(lock: &'a InnerMutex<T>) -> Option<MutexGuard<'a, T>> {