Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,123 @@
|
||||
"""共享文件逐块读取、完整字节校验、EOF 与关闭;输入文件由驱动创建。"""
|
||||
|
||||
|
||||
def source(language):
|
||||
if language == 'dever':
|
||||
return '''setting input: Text
|
||||
loop checksum(bytes: Bytes) (out: Int)
|
||||
| each byte in bytes | result |
|
||||
| out | sum byte |
|
||||
rule sumChunk(state: dever.io.ReadState) (out: Int)
|
||||
| state | out |
|
||||
| Read(bytes) | checksum(bytes) |
|
||||
| End | fail(Invalid) |
|
||||
flow readChunk(file: dever.system.File) (out: Int)
|
||||
| step | kind | do |
|
||||
| state | flow | dever.io.read(file, 64) |
|
||||
| out | rule | sumChunk(state) |
|
||||
loop readChunks(file: dever.system.File, n: Int) (out: Int)
|
||||
| each index in range(0, n) | result |
|
||||
| out | sum readChunk(file) |
|
||||
rule ended(state: dever.io.ReadState) (out: Bool)
|
||||
| state | out |
|
||||
| Read(bytes) | false |
|
||||
| End | true |
|
||||
flow measure(n: Int) (out: Int)
|
||||
| step | kind | do |
|
||||
| file | flow | dever.io.open(input) |
|
||||
| out | loop | readChunks(file, n) |
|
||||
| state | flow | dever.io.read(file, 64) |
|
||||
| end | rule | ended(state) |
|
||||
| - | check | end else Invalid |
|
||||
| - | flow | dever.io.close(file) |
|
||||
cmd bench = measure
|
||||
'''
|
||||
if language == 'rust':
|
||||
return '''use bytes::Bytes;
|
||||
use serde::Deserialize;
|
||||
use std::{fs, io::{self, Read}, sync::{Arc, Mutex}};
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Root { module: Module }
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Module { main: Domain }
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Domain { main: Settings }
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Settings { input: String }
|
||||
struct File(Arc<Mutex<Option<fs::File>>>);
|
||||
fn closed() -> io::Error { io::Error::new(io::ErrorKind::BrokenPipe, "file is closed") }
|
||||
impl File {
|
||||
fn read(&self, limit: i64) -> io::Result<Option<Bytes>> {
|
||||
let limit = usize::try_from(limit).ok().filter(|n| *n > 0)
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "read limit must be positive"))?;
|
||||
let mut guard = self.0.lock().map_err(|_| io::Error::other("file lock poisoned"))?;
|
||||
let file = guard.as_mut().ok_or_else(closed)?;
|
||||
let mut bytes = Vec::new();
|
||||
bytes.try_reserve_exact(limit).map_err(io::Error::other)?;
|
||||
bytes.resize(limit, 0);
|
||||
let count = file.read(&mut bytes)?;
|
||||
bytes.truncate(count);
|
||||
Ok((count != 0).then(|| Bytes::from(bytes)))
|
||||
}
|
||||
fn close(&self) -> io::Result<()> {
|
||||
let file = self.0.lock().map_err(|_| io::Error::other("file lock poisoned"))?.take().ok_or_else(closed)?;
|
||||
drop(file);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
let n: i64 = std::env::args().nth(1).unwrap().parse().unwrap();
|
||||
let config = fs::read_to_string(std::env::args().nth(2).unwrap()).unwrap();
|
||||
let root: Root = serde_json::from_str(&config).unwrap();
|
||||
let file = File(Arc::new(Mutex::new(Some(fs::File::open(root.module.main.main.input).unwrap()))));
|
||||
let mut out = 0i64;
|
||||
for _ in 0..n {
|
||||
let bytes = file.read(64).unwrap().expect("premature EOF");
|
||||
let mut sum = 0i64;
|
||||
for byte in bytes { sum = sum.checked_add(i64::from(byte)).unwrap(); }
|
||||
out = out.checked_add(sum).unwrap();
|
||||
}
|
||||
assert!(file.read(64).unwrap().is_none());
|
||||
file.close().unwrap();
|
||||
println!("{out}");
|
||||
}
|
||||
'''
|
||||
return '''package main
|
||||
import ("encoding/json"; "fmt"; "io"; "os"; "strconv"; "sync")
|
||||
type File struct { mu sync.Mutex; file *os.File }
|
||||
func (file *File) read(limit int) []byte {
|
||||
if limit <= 0 { panic("read limit must be positive") }
|
||||
file.mu.Lock(); defer file.mu.Unlock()
|
||||
if file.file == nil { panic("file is closed") }
|
||||
buffer := make([]byte, limit)
|
||||
n, err := file.file.Read(buffer)
|
||||
if err != nil && err != io.EOF { panic(err) }
|
||||
return buffer[:n]
|
||||
}
|
||||
func (file *File) close() {
|
||||
file.mu.Lock(); defer file.mu.Unlock()
|
||||
if file.file == nil { panic("file is closed") }
|
||||
if err := file.file.Close(); err != nil { panic(err) }; file.file = nil
|
||||
}
|
||||
func main() {
|
||||
n, err := strconv.ParseInt(os.Args[1], 10, 64); if err != nil { panic(err) }
|
||||
input, err := os.Open(os.Args[2]); if err != nil { panic(err) }
|
||||
var config struct { Module struct { Main struct { Main struct { Input string } } } }
|
||||
decoder := json.NewDecoder(input); decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&config); err != nil { panic(err) }; input.Close()
|
||||
opened, err := os.Open(config.Module.Main.Main.Input); if err != nil { panic(err) }
|
||||
file := File{file: opened}; out := int64(0)
|
||||
for index := int64(0); index < n; index++ {
|
||||
bytes := file.read(64); if len(bytes) == 0 { panic("premature EOF") }
|
||||
sum := int64(0)
|
||||
for _, value := range bytes { next := sum + int64(value); if next < sum { panic("overflow") }; sum = next }
|
||||
next := out + sum; if next < out { panic("overflow") }; out = next
|
||||
}
|
||||
if len(file.read(64)) != 0 { panic("expected EOF") }; file.close()
|
||||
fmt.Println(out)
|
||||
}
|
||||
'''
|
||||
Reference in new issue
Block a user