-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: rename program to starknet * chore(makefile): add pre-commit * fix: format issues * feat: add zero parser and test (#23) * Remove unused package * Update Json structure * Update parsing tests --------- Co-authored-by: Rodrigo <rodrodpino@gmail.com>
- Loading branch information
1 parent
4949807
commit b8004e5
Showing
10 changed files
with
562 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package zero | ||
|
||
import ( | ||
"encoding/json" | ||
starknetParser "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" | ||
"os" | ||
) | ||
|
||
type FlowTrackingData struct { | ||
ApTracking ApTracking `json:"ap_tracking"` | ||
ReferenceIds map[string]uint64 `json:"reference_ids"` | ||
} | ||
|
||
type ApTracking struct { | ||
Group int `json:"group"` | ||
Offset int `json:"offset"` | ||
} | ||
|
||
type Location struct { | ||
EndCol uint64 `json:"end_col"` | ||
EndLine uint64 `json:"end_line"` | ||
InputFile map[string]string `json:"input_file"` | ||
StartCol uint64 `json:"start_col"` | ||
StartLine uint64 `json:"start_line"` | ||
} | ||
|
||
type HintLocation struct { | ||
Location Location `json:"location"` | ||
NPrefixNewlines uint64 `json:"n_prefix_newlines"` | ||
} | ||
|
||
type InstructionLocation struct { | ||
AccessibleScopes []string `json:"accessible_scopes"` | ||
FlowTrackingData FlowTrackingData `json:"flow_tracking_data"` | ||
Hints []HintLocation `json:"hints"` | ||
Inst Location `json:"inst"` | ||
} | ||
|
||
type DebugInfo struct { | ||
FileContents map[string]string `json:"file_contents"` | ||
InstructionLocations map[string]InstructionLocation `json:"instruction_locations"` | ||
SourceCode string `json:"source_code"` | ||
SourcePath string `json:"source_path"` | ||
} | ||
|
||
type Hint struct { | ||
AccessibleScopes []string `json:"accessible_scopes"` | ||
Code string `json:"code"` | ||
FlowTrackingData FlowTrackingData `json:"flow_tracking_data"` | ||
} | ||
|
||
type Reference struct { | ||
ApTrackingData ApTracking `json:"ap_tracking_data"` | ||
Pc uint64 `json:"pc"` | ||
Value string `json:"value"` | ||
} | ||
|
||
type ReferenceManager struct { | ||
References []Reference | ||
} | ||
|
||
type AttributeScope struct { | ||
StartPc uint64 `json:"start_pc"` | ||
EndPc uint64 `json:"end_pc"` | ||
FlowTrackingData FlowTrackingData `json:"flow_tracking_data"` | ||
AccessibleScopes []string `json:"accessible_scopes"` | ||
} | ||
|
||
type ZeroProgram struct { | ||
Prime string `json:"prime"` | ||
Data []string `json:"data"` | ||
Builtins []starknetParser.Builtin `json:"builtins"` | ||
Hints map[string][]Hint `json:"hints"` | ||
CompilerVersion string `json:"version"` | ||
MainScope string `json:"main_scope"` | ||
Identifiers map[string]any `json:"identifiers"` | ||
ReferenceManager ReferenceManager `json:"reference_manager"` | ||
Attributes []AttributeScope `json:"attributes"` | ||
DebugInfo DebugInfo `json:"debug_info"` | ||
} | ||
|
||
func (z ZeroProgram) MarshalToFile(filepath string) error { | ||
// Marshal Output struct into JSON bytes | ||
data, err := json.MarshalIndent(z, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write JSON bytes to file | ||
err = os.WriteFile(filepath, data, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ZeroProgramFromFile(filepath string) (zero *ZeroProgram, err error) { | ||
content, err := os.ReadFile(filepath) | ||
if err != nil { | ||
return | ||
} | ||
return ZeroProgramFromJSON(content) | ||
} | ||
|
||
func ZeroProgramFromJSON(content json.RawMessage) (*ZeroProgram, error) { | ||
var zero ZeroProgram | ||
return &zero, json.Unmarshal(content, &zero) | ||
} |
Oops, something went wrong.