from __future__ import annotations
from dataclasses import dataclass
from typing import (
Any, Callable, Generic, List, Optional, Sequence, Tuple, TypeVar, cast
)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::mem; | |
use std::ptr; | |
fn main() { | |
// x86_64 machine code: write(1, "hello world\n", 12) | |
const CODE: &[u8] = &[ | |
0xb8, 0x01, 0x00, 0x00, 0x00, // mov eax, 1 | |
0xbf, 0x01, 0x00, 0x00, 0x00, // mov edi, 1 | |
0x48, 0x8d, 0x35, 0x08, 0x00, 0x00, 0x00, // lea rsi, [rip+0x8] | |
0xba, 0x0c, 0x00, 0x00, 0x00, // mov edx, 12 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::arch::x86_64::__cpuid; | |
unsafe fn get_vendor_name() -> String { | |
let vendor_name = __cpuid(0); | |
let mut vendor_name_string = String::with_capacity(12); | |
vendor_name_string.push_str(&String::from_utf8_lossy(&vendor_name.ebx.to_le_bytes())); | |
vendor_name_string.push_str(&String::from_utf8_lossy(&vendor_name.edx.to_le_bytes())); | |
vendor_name_string.push_str(&String::from_utf8_lossy(&vendor_name.ecx.to_le_bytes())); | |
vendor_name_string | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cell::UnsafeCell; | |
use std::sync::atomic::{AtomicBool, Ordering}; | |
static UNLOCKED: bool = false; | |
static LOCKED: bool = true; | |
struct SpinLock<T> { | |
inner: UnsafeCell<T>, | |
state: AtomicBool, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
sys := __import__("sys"), | |
your_value := input(), | |
factorial := lambda value: 1 if not value else factorial(value-1) * value, | |
( | |
print("Error: Your input is not a decimal number"), | |
sys.exit(1), | |
) | |
if not str.isdecimal(your_value) | |
else (result := factorial(int(your_value))), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
(lambda fibonacci: lambda v, then: fibonacci(fibonacci, v, then)) | |
( | |
lambda fibonacci, value, then: | |
then(1) | |
if | |
value == 0 or value == 1 | |
else | |
fibonacci( | |
fibonacci, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from dataclasses import dataclass | |
from typing import Callable, Generic, TypeVar, cast, NoReturn, Optional, Any | |
I = TypeVar("I") | |
O = TypeVar("O") | |
R = TypeVar("R") | |
T = TypeVar("T") |
Here's my code:
Parser = Callable[[str, State], ParseResult[T]]
def monadic_fail(parser: Parser[T]) -> Parser[T]:
def new_parser(stream: str, state: State) -> ParseResult[T]:
if isinstance(state, ParseError):
return ParseResult(output=None, state=state)
return parser(stream, state)
return new_parser